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        businessbloomer.com/woocommerce-customization
 * @author        Rodolfo Melogli, Business Bloomer
 * @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

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

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

  1. Hello Rodolfo, would you think it’s possible to add a shortcode in the tab section
    echo ‘Whatever content for’
    or does it need further coding?

    1. Yes! You can just use this in PHP:

      echo do_shortcode( ['your_shortcode'] );
      1. ….thank you, it works! Would you help me with a little custom edit, if I want to echo a different shortcode when the browser is in a different language? Would it be possible to do something like “if url is domain.com/eng/ then echo shortcode 2?
        I’d appreciate if you write some code for me. If not, I thank you anyway.

        1. Great! If you use a multi-language plugin, I’m sure they have a function you can use to do conditional work πŸ™‚

  2. Hi Rodolfo! Many thanks for this; it works perfectly. One question: I want the tab to be open by default. How can I make this happen? Thank you!

    1. Hi Maria! Would it work if your custom tab went to position 1?

  3. 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?

  4. 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 *