WooCommerce: Add New Tab @ Single Product Page

I find it unusual that after years of sharing tutorials on Business Bloomer there is no snippet for creating a custom product tab on the single product page!

I mean, we’ve seen an example about Related Products in a Custom Tab snippet a while ago, but here I want to concentrate on the actual functionality of adding a custom tab, giving a name and a heading to it, showing it conditionally (e.g. only for a specific product category), and making sure the “scroll to tab” works when the URL contains its anchor tag.

Enjoy!

If you go to any Business Bloomer plugin product page, you will see the below code in action: I added a custom product tab called “Docs”, and placed some content in it. Bonuses: it only shows for a given product category, and also there is some JS that allow customers to scroll to the tab on load in case the URL contains the correct has.

PHP Snippet: Create a Custom Product Tab @ WooCommerce Single Product Page

/**
 * @snippet       New Product Tab @ WooCommerce Single Product
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 8
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_product_tabs', 'bbloomer_add_product_tab', 9999 );
  
function bbloomer_add_product_tab( $tabs ) {
	$tabs['docs'] = array(
		'title' => __( 'Docs', 'woocommerce' ), // TAB TITLE
		'priority' => 50, // TAB SORTING (DESC 10, ADD INFO 20, REVIEWS 30)
		'callback' => 'bbloomer_docs_product_tab_content', // TAB CONTENT CALLBACK
   );
   return $tabs;
}

function bbloomer_docs_product_tab_content() {
	global $product;
	echo 'Whatever content for ' . $product->get_name();
}

Bonus 1: Display Custom Product Tab Conditionally @ WooCommerce Single Product Page

For example, you may want to show the custom tab only if the current product belongs to the product category ‘plugins’. Simply wrap the previous declaration inside an has_term conditional:

function bbloomer_add_product_tab( $tabs ) {
	if ( has_term( 'plugins', 'product_cat', wc_get_product()->get_id() ) ) {
		$tabs['docs'] = array(
			'title' => __( 'Docs', 'woocommerce' ), // TAB TITLE
			'priority' => 50, // TAB SORTING (DESC 10, ADD INFO 20, REVIEWS 30)
			'callback' => 'bbloomer_docs_product_tab_content', // TAB CONTENT
		);
	}
   return $tabs;
}

Bonus 2: Scroll to Custom Product Tab If URL Contains Anchor Tag @ WooCommerce Single Product Page

If you redirect users to example.com/whatever-product/#tab-docs, where tab-docs is the anchor tag of the brand new product tab, the following edit will allow you to scroll to it on load.

Unfortunately the default WooCommerce JS does not have this function for custom tabs, so we need to code it ourselves:

function bbloomer_docs_product_tab_content() {
	global $product;
	echo 'Whatever content for ' . $product->get_name();
   wc_enqueue_js( "
		$( 'body' ).on( 'maybescroll', '.woocommerce-tabs', function() {
			var hash = window.location.hash;
			if ( hash === '#tab-docs' ) {
				$( this ).find( 'li.docs_tab a' ).trigger( 'click' );
			} 
		});
		$( '.woocommerce-tabs' ).trigger( 'maybescroll' );
	" );
}

Advanced Plugin: WooCommerce Product Tabs

In case you don’t want to code or edit PHP, I also researched plugins which will allow you to do the same thing as the above code snippet. I found that Barn2’s WooCommerce Product Tabs plugin has this exact feature, plus a lot more too.

This robust plugin lets you add an unlimited number of extra tabs to the single product page. As with the code snippet, you can put whatever you like in each tab. It also has extra features such as letting you hide or rename the default tabs, and add an icon to each tab:

Here’s how you can add a new tab, and tab content, to the WooCommerce Single Product page thanks to the Barn2 WooCommerce Product Tabs plugin

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Move Product Tabs Under the Short Description
    When the long description is very short, you may want to move the whole block of “product tabs” to the right hand side of the product image – as opposed to wasting a lot of space underneath it. So, here’s how you can remove the product tabs from their original position, and display them beside […]
  • WooCommerce: Remove Additional Information Tab @ Single Product
    The “Additional Information” tab on the single product page is somewhat annoying and, honestly, quite useless. There are 2 simple methods to “hide” it or delete it completely: a CSS and a PHP solution. In the first case, you can input the code in your style.css; in the second case use your child theme’s functions.php. […]
  • WooCommerce: Remove Product Tabs, Show Long Description
    Today we take a look at the WooCommerce Single Product page and specifically at how to remove the whole Product Tabs Section, and re-add the Long Description on its own (and not inside a tab).
  • WooCommerce: Remove “Description” Heading @ Single Product Tabs
    When you are on the single product page, and you have a non-empty product long description, a “Description” tab appears below the product images. Unfortunately, not only the tab label is “Description”, but also the tab H2 heading. This sounds and looks horrible, so here’s a way to completely remove it. Alternatively, you can use […]
  • WooCommerce: Scroll To Product Tab @ Single Product Page
    Depending on your theme, just creating an href link anchor to a product tab might or might not work i.e. it might not scroll to it as it’s currently closed. Here comes a way to create href links that not only scroll to the tab, but also open it in case it’s closed (this will […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

4 thoughts on “WooCommerce: Add New Tab @ Single Product Page

  1. Hey, it worked perfectly. The problem is that I can’t change the order of the tabs. Any solution? Many thanks!

    1. Hey Marius, did you play with the “priority” value?

  2. Hello Rodolfo! Thank you for making Woocommerce more understandable for my kind of noobie. Your code works great to create an extra custom single product page tab. Very nice! My problem is that I cannot figure out how to add a shortcode running a tiny 3rd party plugin importing texts etc. from another database. The shortcode and plugin work fine (tested) but how should it coded to be run showing the information on the custom tab?

    It seems there need to more coding here, but I have no idea what, LOL

    1. Hello Era! What’s the shortcode you need to add?

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *