排除在相关产品的具体产品标签WooCommerce 3+
问题描述:
我用下面的命令行排除特定标签的产品相关WooCommerce产品:排除在相关产品的具体产品标签WooCommerce 3+
add_filter('woocommerce_get_related_product_tag_terms', 'remove_related_tags');
function remove_related_tags($terms) {
foreach ($terms as $key => $term) {
if ('Đồng Hồ Thụy Sỹ' === $term->name) {
unset($terms[ $key ]);
}
if ('dong-ho-thuy-sy' === $term->slug) {
unset($terms[ $key ]);
}
if ('Đồng Hồ 6 Kim' === $term->name) {
unset($terms[ $key ]);
}
if ('Citizen Eco-Drive' === $term->name) {
unset($terms[ $key ]);
}
if ('Seiko Kinetic' === $term->name) {
unset($terms[ $key ]);
}
if ('Seiko Solar' === $term->name) {
unset($terms[ $key ]);
}
if ('Đồng Hồ Dây Da Nam Nữ' === $term->name) {
unset($terms[ $key ]);
}
}
return $terms;
}
但由于WooCommerce更新3版本的代码不工作了并没有效果。
有没有其他方法可以从相关产品中排除特定标签产品?
答
由于woocommerce 3,事情到处都有变化。现在,在这个钩子中,不再有产品标签术语对象的数组,而只有术语ID的数组......这就是为什么你的代码不工作。
它应该工作与替换它:
add_filter('woocommerce_get_related_product_tag_terms', 'remove_related_tags', 10, 2);
function remove_related_tags($tag_terms_ids, $product_id ){
// Get the product tag term object by field type and converting it to term ID
function get_term_id_by($value, $type, $tax = 'product_tag'){
return get_term_by($type, $value, $tax)->term_id;
}
// Set here the product tag term by field type to exclude
$exclude_terms_ids = array(
get_term_id_by('Đồng Hồ Thụy Sỹ', 'name'),
get_term_id_by('dong-ho-thuy-sy', 'slug'),
get_term_id_by('Đồng Hồ 6 Kim', 'name'),
get_term_id_by('Citizen Eco-Drive', 'name'),
get_term_id_by('Seiko Kinetic', 'name'),
get_term_id_by('Seiko Solar', 'name'),
get_term_id_by('Đồng Hồ Dây Da Nam Nữ', 'name'),
);
// Removing the necessary product tag terms IDs from the array
foreach ($tag_terms_ids as $key => $term_id)
foreach ($exclude_terms_ids as $exclude_term_id)
if ($term_id == $exclude_term_id)
unset($tag_terms_ids[ $key ]);
// Return the custom array of product tag terms IDs
return $tag_terms_ids;
}
代码放在您的活动子主题(或主题)的function.php文件或也以任何插件文件。
但不幸的是,它似乎没有因为WooCommerce 3
你好卢瓦克TheAztec有关
woocommerce_get_related_product_tag_terms
和woocommerce_get_related_product_cat_terms
错误将无法正常工作,谢谢您的支持。我试过了,但似乎没有奏效。没有任何效果。你有其他想法吗? –Hello @ĐặngHảiTriều我已经做了一些进一步的测试,看起来这个钩子有一个bug。所以它不可能让他们工作。 – LoicTheAztec