取消设置woocommerce中特定产品类别的产品标签

时间:2017-08-22 09:23:39

标签: php wordpress woocommerce categories product

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

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;

}

此代码可以正常设置或隐藏特定产品的标签。

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

如何为产品类别执行此操作?

2 个答案:

答案 0 :(得分:2)

作为最初来自我的一个答案的代码,只需更改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 = method_exists( $product, 'get_id' ) ? $product->get_id() : $product->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,slugs或名称......

答案 1 :(得分:1)

尝试以下代码

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个类别

如需查看更多,则使用has_term()

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