Menu Close

WooCommerce: Add New Tab @ Single Product Page

default

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 on 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 7
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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' );
	" );
}
View Source
Posted in WooCommerce Tips