仅在 Woocommerce 3 中根据标签显示相关产品

Display related products based on tags only in Woocommerce 3

默认情况下,单个产品页面上的 Woocommerce 根据类别显示相关产品 - 相关产品滑块将显示相同类别的产品。

Woocommerce 文档说明了这一点 - "Related Products is a section on some templates that pulls products from your store that share the same tags or categories as the current product. These products cannot be specified in the admin, but can be influenced by grouping similar products in the same category or by using the same tags."

我的商店有 2 个类别 - "men" 和 "women",所以这个与 Woocommerce 相关的产品条件对我不起作用。我的相关产品应该更具体,因此我添加了标签。

所以 - 我想只根据标签而不是类别来显示相关产品。我该怎么做?

此论坛上也有类似的问题,但 none 提供的代码有效,可能是因为 Woocommerce 更新了几次。

谢谢!

这可以用这个小钩子函数来完成:

add_filter( 'woocommerce_get_related_product_cat_terms', 'remove_related_product_categories', 10, 2 );
function remove_related_product_categories( $terms_ids, $product_id  ){
    return array();
}

代码进入活动子主题(或活动主题)的 function.php 文件。

已测试并有效。