Sometimes those three default Woocommerce product tabs are not enough form you and you need to add your own custom tabs. There are a lot of plugins available for this but why add another plugin if you can do it with the couple of lines of copy and pasting. So, if you want to know how to create, remove, rename and reorder Woocommerce product tabs then keep reading.
How to remove Woocommerce product tabs?
Take a look at this code here below. Just copy it and paste it you your child theme’s function.php file. I would not recommend it though since you’ll lose custom tabs after switching your theme in the future. If you need a solution which does not depend on the theme then install a Code Snippets plugin which lets you add these kinds of snippets without any worries.
add_filter( 'woocommerce_product_tabs', 'woo_remove_product_tabs', 98 );
function woo_remove_product_tabs( $tabs ) {
unset( $tabs['description'] ); // Remove the description tab
unset( $tabs['reviews'] ); // Remove the reviews tab
unset( $tabs['additional_information'] ); // Remove the additional information tab
return $tabs;
}
How to rename Woocommerce product tabs?
Take a look at the code here below and add it to your Code snippets.
add_filter( 'woocommerce_product_tabs', 'woo_rename_tabs', 98 );
function woo_rename_tabs( $tabs ) {
$tabs['description']['title'] = __( 'More Information' ); // Rename the description tab
$tabs['reviews']['title'] = __( 'Ratings' ); // Rename the reviews tab
$tabs['additional_information']['title'] = __( 'Product Data' ); // Rename the additional information tab
return $tabs;
}
How to re-order Woocommerce product tabs?
There are times when you just need to re-order your Woocommerce product tabs. If so, then use this code here below. Take a look at the priority numbers (5, 10 and 15). Tab with the lowest priority number is shown first.
add_filter( 'woocommerce_product_tabs', 'woo_reorder_tabs', 98 );
function woo_reorder_tabs( $tabs ) {
$tabs['reviews']['priority'] = 5; // Reviews first
$tabs['description']['priority'] = 10; // Description second
$tabs['additional_information']['priority'] = 15; // Additional information third
return $tabs;
}
How to create custom Woocommerce product tab?
Now lets create a custom Woocommerce product tab which is shown for all products. We’ll call this tab a “Measurement charts”
add_filter( 'woocommerce_product_tabs', 'custom_tab' );
function custom_tab( $tabs ) {
// Adds the new tab
$tabs['test_tab'] = array(
'title' => __( 'Measurement charts', 'woocommerce' ),
'priority' => 50,
'callback' => 'custom_tab_content'
);
return $tabs;
}
function custom_tab_content() {
// The new tab content
echo '<h2>Measurement charts</h2>';
echo '<p>Lorem ipsum dolor sit amet consectetur adipiscing elit ultricies convallis volutpat, placerat proin scelerisque eget velit tellus at nibh risus. </p>';
}
How to create Woocommerce Conditional Product Tabs?
Well, sometimes you just need to show a custom product tab for one product and another one for other products. Therefore, in this example I am creating two custom tabs and I‘m going to show then condtiionally.
- first custom product tab is shown only for products in Music category
- second custom product tab is shown only for products in Pants category
// Conditional tabs
add_filter( 'woocommerce_product_tabs', 'custom_tabs' );
function custom_tabs( $tabs ) {
// Adds the new tab
if ( has_term( 'music', 'product_cat' ) ) { // Shows tab only for Music category. Here goes the category slug
$tabs['enquiry'] = array(
'title' => __( 'Send enquiry', 'woocommerce' ),
'priority' => 50,
'callback' => 'custom_tab_content1'
);
}
elseif ( has_term( 'pants', 'product_cat' ) ) { // Shows tab only for Decor category. Here goes the category slug
$tabs['enquiry'] = array(
'title' => __( 'Shipping information', 'woocommerce' ),
'priority' => 50,
'callback' => 'custom_tab_content1'
);
}
return $tabs;
}
function custom_tab_content1() {
// The new tab content
if ( has_term( 'music', 'product_cat' ) ) { // Shows tab only for Music category. Here goes the category slug
echo '<h2>Send enquiry</h2>';
echo '<p>Lorem ipsum dolor sit amet consectetur adipiscing elit nostra egestas, ultrices sociosqu maecenas netus lacinia id magna porta proin, vel justo imperdiet aliquet volutpat aliquam risus elementum.</p>';
echo do_shortcode('');
}
elseif ( has_term( 'pants', 'product_cat' ) ) { // Shows tab only for Decor category. Here goes the category slug
echo do_shortcode('[reblex id="1346"]');
}
}
How to move Woocommerce single product short description under a tab
This shortcode will remove Woocommerce single product short description, then creates a new tab called Short description and moves short description over there.
// Remove short description
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_excerpt', 20 );
// Add short description to a custom product tab
add_filter( 'woocommerce_product_tabs', 'add_custom_product_tab', 10, 1 );
function add_custom_product_tab( $tabs ) {
$custom_tab = array(
'custom_tab' => array(
'title' => __( "Short description", "woocommerce" ),
'priority' => 12,
'callback' => 'short_description_tab_content'
)
);
return array_merge( $custom_tab, $tabs );
}
// Custom product tab content
function short_description_tab_content() {
global $post, $product;
$short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );
if ( ! $short_description ) {
return;
}
echo '<div class="woocommerce-product-details__short-description">' . $short_description . '</div>'; // WPCS: XSS ok.
}
How to use shortcodes inside Woocommerce Conditional Product Tabs?
Pay attention though, that you can also use shortcodes inside your custom tabs. For example, I am using Fluent forms for enquiry form tab and Reusable Blocks Extended plugin for showing Gutenberg blocks inside Shipping information tab.
So, if you don’t need to use shortcodes in your tabs then delete this part in the coe above.
echo do_shortcode('');