简体   繁体   中英

Related Products only by categories, not by tags in WooCommerce 3

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' );

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM