首页 文章

在WooCommerce 3中排除相关产品的特定产品标签

提问于
浏览
2

我使用以下命令行从相关的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后,此代码不再起作用并且无效 .

有没有其他方法可以从相关产品中排除特定标签产品?

1 回答

  • 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_get_related_product_tag_terms和woocommerce_get_related_product_cat_terms相关的错误,因为WooCommerce 3

相关问题