Q
Q
Qnex2019-03-20 03:20:59
WordPress
Qnex, 2019-03-20 03:20:59

How to show related products by category and by tag on Woocommerce product page?

Good afternoon,
in WooCommerce you need to display similar products by a tag that is set when filling out and is not known in advance, that is, I just cannot specify it in the code. Products that will have this tag are in different categories, there can be 15-20 of them in total.
I plan to display pictures with the name of similar products in the product card in tabs, where each tab belongs to its category. I just added tabs to the end of the /wp-content/themes/my-theme/woocommerce/single-product/product-image.php file so that they are under the big photo - I am writing this because it is not a standard place for related products and the built-in Woocommerce [products] shortcode tag="hoodie"] does not work there, although it shows everything in the description below (I tried it for verification).
I found several variants of this code, the task occurs, but none of them worked for me. Here's what I think is the closest option, but most likely outdated:

<?php
$product_tag = get_post_meta($post->ID, 'test1', true);
$args = array('post_type' => 'product', 'product_cat' => 'accessories', 'test1' => $product_tag );
$postslist = get_posts( $args );
foreach ($postslist as $post) : setup_postdata($post); ?>
<ul>
<li><?php the_title(); ?></li>
</ul>
<?php endforeach; ?>

test1 - this is the name of the custom field where the tag was added, accessories - the category for this tab here.
But for me, this code displays a list with the same names of the current product.
I myself have never been a programmer, if someone helps I will be grateful, I can pay for the time.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Yanchevsky, 2019-03-20
@deniscopro

Kind.
Here
try
Is something changing?
PS accessories removed for now. Can you have the same tag in different categories?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question