Hiding tabs only for some products in WooCommerce

2019-07-25 03:29发布

问题:

I would like to know how can I hide or remove the WooCommerce tabs only on certain single product pages (keeping them on other pages using css or jquery if that's possible)?

Woocommerce div tabs: class="woocommerce-tabs wc-tabs-wrapper"

回答1:

It's very easy to remove the WooCommerce tabs on certain single products. You can easily do this using CSS. Every WC products have a Unique product id, please check this http://prntscr.com/dp2c79 , you will find this using Browser inspect element or source code. Find product id then Just use 'Display: none' tabs for that product id.

Example: #product-834 .tabs {display: none !important;}

Regards,



回答2:

Is possible to use the dedicated woocommerce_product_tabs filter hook to remove tabs only for some products (on 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;

}

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

This code is tested and works.

Based on original WooCommerce snippet: Editing product data tabs