【问题标题】:Unset product tabs for specific product categories in woocommerce在 woocommerce 中取消设置特定产品类别的产品标签
【发布时间】:2018-01-30 12:11:03
【问题描述】:

我正在使用来自此答案的代码:

Hiding tabs only for some products in WooCommerce single product pages

代码如下:

add_filter( 'woocommerce_product_tabs', 'conditionaly_removing_product_tabs', 98 );
function conditionaly_removing_product_tabs( $tabs ) {

    // Get the global product object
    global $product;

    // Get the current product ID
    $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;

    // Define HERE your targetted products IDs in this array   <===  <===  <===
    $target_products_ids = array(123,152,162);

    // If the current product have the same ID than one of the defined IDs in your array,… 
    // we remove the tab.
    if(in_array($product_id, $target_products_ids)){

        // KEEP BELOW ONLY THE TABS YOU NEED TO REMOVE   <===  <===  <===  <===
        unset( $tabs['description'] ); // (Description tab)  
        unset( $tabs['reviews'] );     // (Reviews tab)
        unset( $tabs['additional_information'] ); // (Additional information tab)

    }

    return $tabs;

}

此代码可以很好地取消设置或隐藏特定产品的标签。

相反,我想取消设置或隐藏特定产品类别的标签。

我怎样才能为产品类别做到这一点?

【问题讨论】:

    标签: php wordpress woocommerce categories product


    【解决方案1】:

    作为我的答案之一的代码,只需更改 2 行并使用 WordPress 条件函数has_term(),使其适用于产品类别非常简单:

    add_filter( 'woocommerce_product_tabs', 'conditionaly_removing_product_tabs', 99 );
    function conditionaly_removing_product_tabs( $tabs ) {
    
        // Get the global product object
        global $product;
    
        // Get the current product ID
        $product_id =  $product->get_id();
    
        // Define HERE your targeted categories (Ids, slugs or names)   <===  <===  <===
        $product_cats = array( 'clothing', 'posters' );
    
        // If the current product have the same ID than one of the defined IDs in your array,… 
        // we remove the tab.
        if( has_term( $product_cats, 'product_cat', $product_id ) ){
    
            // KEEP BELOW ONLY THE TABS YOU NEED TO REMOVE   <===  <===  <===  <===
            unset( $tabs['description'] ); // (Description tab)  
            unset( $tabs['reviews'] );     // (Reviews tab)
            unset( $tabs['additional_information'] ); // (Additional information tab)
        }
        return $tabs;
    }
    

    代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件中。

    经过测试的代码在 WooCommerce 中运行。

    WordPress 条件函数 has_term() 接受您的产品类别的 ID、slug 或名称...

    【讨论】:

      【解决方案2】:

      试试下面的代码

      add_filter( 'woocommerce_product_tabs', 'conditionaly_removing_product_tabs', 99 );
      function conditionaly_removing_product_tabs( $tabs ) {
      
          // Get the global product object
          global $product;
      
          // Get the current product ID
          $product_id = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->id;
      
          //get all categories
          $terms = wp_get_post_terms( $product_id, 'product_cat' );
          foreach ( $terms as $term ) 
          {
                  $categories[] = $term->slug;
          }
          if ( in_array( 'Your-product-categories-slug', $categories ) ) {  
              unset( $tabs['description'] ); // (Description tab)  
              unset( $tabs['reviews'] );     // (Reviews tab)
              unset( $tabs['additional_information'] ); // (Additional information tab) 
          }
          return $tabs;
      }
      

      编辑检查超过 1 个类别

      检查超过 1 个类别使用has_term()

      $product_cats=array('Your-product-categories-slug1','Your-product-categories-slug2','Your-product-categories-slug3');
      if( has_term( $product_cats, 'product_cat', $product_id ) ){
      .....
      }
      

      【讨论】:

      • 先生,如果我们在这段代码中添加更多类别,那么新语法是什么?
      • 您是否为 2 个或更多类别执行此操作?
      • 是的先生超过 2 个类别..实际上我总共有超过 25 个类别
      • 你需要添加if ( in_array( 'Your-product-categories-slug1', $categories ) || in_array( 'Your-product-categories-slug2', $categories ) ) {这样的条件
      猜你喜欢
      • 2021-05-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-11-07
      相关资源
      最近更新 更多