简体   繁体   English

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

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

I use the following command line to exclude specific tags products from related WooCommerce products: 我使用以下命令行从相关的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;
}

But since WooCommerce update version 3 this code doesn't work anymore and has no effect. 但是由于WooCommerce更新版本3,此代码不再起作用,并且无效。

Is there any other way I can exclude specific tags products from related products? 还有什么其他方法可以从相关产品中排除特定标签产品?

Since woocommerce 3, things have change everywhere. 自woocommerce 3以来,情况无处不在。 Now in that hook there is not anymore an array of the product tag terms objects, but only an array of the terms IDs… That's why your code is not working. 现在,在该挂钩中,不再有产品标签术语对象数组,而只有术语ID数组……这就是为什么您的代码无法正常工作的原因。

It should work replacing it with: 它应该可以替换为:

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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file. 代码在您的活动子主题(或主题)的function.php文件中,或者在任何插件文件中。

But Unfortunately it will not work as it seems that there is a bug related to woocommerce_get_related_product_tag_terms and woocommerce_get_related_product_cat_terms since WooCommerce 3 但是遗憾的是,由于自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