First things first. I am NOT proficient in PHP but I can sometimes modify it to do what I want.
I have a WooCommerce site with thousands of products. These are broken down into multiple "Brands". These "Brands" come from an additional plugin called "Perfect Brands WooCommerce" but I have used the "Brand" name as a category as well so I can utilise it.
I found some code on StackOverflow to enable me to display a Tab based on the category of the product, which I then modified for the various categories. As you can see, anything in the "bisley-workwear" category will show the "additional_brand_info_tab" with the content from the "bisley-workwear". The same goes for the other two categories.
What I want to do is show more than one tab for a particular category. i.e. if the product category is "bisley-workwear" I want to show multiple tabs. These would change depending on the category.
Any help would be greatly appreciated.
// Conditional Brand Info Tabs
add_filter( 'woocommerce_product_tabs', 'custom_tabs' );
function custom_tabs( $tabs ) {
// Adds the new tab
if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // // Shows this tab if the product is in the "bisley-workwear" category (Use category slug).
$tabs['additional-brand-info'] = array(
'title' => __( 'Bisley Workwear', 'woocommerce' ),
'priority' => 50,
'callback' => 'additional_brand_info_tab'
);
}
elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the product is in the "as-colour" category (Use category slug).
$tabs['additional-brand-info'] = array(
'title' => __( 'AS Colour', 'woocommerce' ),
'priority' => 50,
'callback' => 'additional_brand_info_tab'
);
}
elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "syzmik-workwear" category (Use category slug).
$tabs['additional-brand-info'] = array(
'title' => __( 'Sizmik Workwear', 'woocommerce' ),
'priority' => 50,
'callback' => 'additional_brand_info_tab'
);
}
return $tabs;
}
function additional_brand_info_tab() {
// The new tab content
if ( has_term( 'bisley-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "bisley-workwear" category (Use category slug).
echo 'This is Bisley Workwear';
}
elseif ( has_term( 'as-colour', 'product_cat' ) ) { // Shows this tab if the product is in the "as-colour" category (Use category slug).
echo do_shortcode('This is AS Colour');
}
elseif ( has_term( 'syzmik-workwear', 'product_cat' ) ) { // Shows this tab if the product is in the "syzmik-workwear" category (Use category slug).
echo ('This is Syzmik Workwear');
}
}
I’m not really sure what to do after modifying the code as above. At the moment this works for displaying one tab based on a category. Multiple tabs would be great.
2
Answers
So, thanks to Snuffy's prompting, I have found a solution. I'm not sure if this is the correct syntax or whether it could be done better, but it works.
This code will display two tabs if a product belongs to the "bisley-workwear" category:
Note the addition of:
and
And the final working code which displays 2 tabs based on the category of the product: