简体   繁体   English

相关产品仅按类别分类,不按WooCommerce 3中的标签分类

[英]Related Products only by categories, not by tags in WooCommerce 3

Trying to display related products in WooCommerce by categories only. 尝试仅按类别显示WooCommerce中的相关产品。 At present WooCommerce uses tags and categories. 目前,WooCommerce使用标签和类别。

We've tried implementing the following, but I'm guessing over time this will have stopped working perhaps owing to updates on WooCommerce side. 我们已经尝试实现以下内容,但是随着时间的推移,我猜想这可能会因为WooCommerce方面的更新而停止工作。 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: 查看第842行的相关核心代码 ,您可以看到:

$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 . 您会注意到第二个过滤器挂钩: 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. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

This is tested and works. 这已经过测试并且可以工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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