2017-08-25 145 views
2

我用下面的命令行排除特定標籤的產品相關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版本的代碼不工作了並沒有效果。

有沒有其他方法可以從相關產品中排除特定標籤產品?

回答

0

由於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

+0

你好盧瓦克TheAztec有關woocommerce_get_related_product_tag_termswoocommerce_get_related_product_cat_terms錯誤將無法正常工作,謝謝您的支持。我試過了,但似乎沒有奏效。沒有任何效果。你有其他想法嗎? –

+0

Hello @ĐặngHảiTriều我已經做了一些進一步的測試,看起來這個鉤子有一個bug。所以它不可能讓他們工作。 – LoicTheAztec