Related Products only by categories, not by tags i

2019-07-26 19:03发布

Trying to display related products in WooCommerce by categories only. At present WooCommerce uses tags and categories.

We've tried implementing the following, but I'm guessing over time this will have stopped working perhaps owing to updates on WooCommerce side. It now no longer works in any theme we've tested.

add_filter( 'woocommerce_product_related_posts_relate_by_tag', '__return_false' );

1条回答
倾城 Initia
2楼-- · 2019-07-26 19:33

NOTE: this hook is working again now.

A functional alternative:

Looking at the related core code at line 842 you have this:

$tags_array = apply_filters( 'woocommerce_product_related_posts_relate_by_tag', true, $product_id ) ? apply_filters( 'woocommerce_get_related_product_tag_terms', wc_get_product_term_ids( $product_id, 'product_tag' ), $product_id ) : array();

You will notice a second filter hook: woocommerce_get_related_product_tag_terms. So may be you can try to use instead this code:

add_filter( 'woocommerce_get_related_product_tag_terms', function( $term_ids, $product_id ){
    return array();
}, 10, 2 );

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

This is tested and works.

查看更多
登录 后发表回答