簡體   English   中英

從WooCommerce 3+中的相關產品中排除特定產品標簽

[英]Exclude specific product tags from related products in WooCommerce 3+

我使用以下命令行從相關的WooCommerce產品中排除特定的標簽產品:

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以來似乎存在與woocommerce_get_related_product_tag_termswoocommerce_get_related_product_cat_terms相關的錯誤,因此無法正常工作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM