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

落花浮王杯 提交于 2019-12-11 06:37:50

问题


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:


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.



来源:https://stackoverflow.com/questions/45698470/related-products-only-by-categories-not-by-tags-in-woocommerce-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!