I would like to remove an explicit tab in our shop. Generally no problem either. But: I would like to keep it in one category (ID = 15)
I found this code to remove the tabs on special pages
what needs to be changed now?
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;
}```
Ok, I am not a woocommerce expert and neither have worked with it. However, knowing wordpress and how get terms work .. you could try some thing as follow.
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' );
// lets get all terms and then programatically exclude certain categories
$terms = get_terms( 'product_cat', array(
'orderby' => 'name',
'order' => 'ASC',
'exclude' => array( 77, 71 ), // dont forget to include ids for the categories
));
// Loop over the tabs and remove them from the categories
array_map(function($term) use ($product_id, $tabs){
if ( has_term($term, 'product_cat', $product_id)){
unset( $tabs['description'] ); // (Description tab)
unset( $tabs['reviews'] ); // (Reviews tab)
unset( $tabs['additional_information'] ); // (Additional information tab)
}
}, $terms);
return $tabs;
}
thanks for your answer! But it doesnt work... maybe we have to be different here ...
I have a code in the functions.php with which I add a custom tab. (This is also the tab that should only be displayed "ONLY" in the future in a certain product category.
//Preisvorschlag Produkt-Tab hinzufügen
add_filter( 'woocommerce_product_tabs', 'wp_bibel_de_add_woocommerce_product_tabs' );
function wp_bibel_de_add_woocommerce_product_tabs( $tabs ) {
$tabs['wp_bibel_de_custom_tab'] = array(
'title' => __( 'Requests' ),
'priority' => 50,
'callback' => 'wp_bibel_de_new_product_tab_callback'
);
return $tabs;
}
the category ID in which this tab should be displayed is: 228
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.