Answer the question
In order to leave comments, you need to log in
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; ?>
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question