Unset product tabs for specific product categories

2019-01-20 15:47发布

I am using the code coming from this answer:

Hiding tabs only for some products in WooCommerce single product pages

Here is that code:

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;

}

This code works fine to unset or hide tabs from specific products.

Instead I would like to unset or hide tabs from specific product categories.

How can I do it for product categories?

2条回答
兄弟一词,经得起流年.
2楼-- · 2019-01-20 16:07

Try below code

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

Edit for check more then 1 categories

For check more then 1 categories used 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 ) ){
.....
}
查看更多
该账号已被封号
3楼-- · 2019-01-20 16:12

As the code originally from one of my answers, it's very simple to make it work for product categories just changing 2 lines and using WordPress conditional function 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;
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

The code tested and works in WooCommerce.

WordPress conditional function has_term() accept Ids, slugs or names of your product categories…

查看更多
登录 后发表回答