Você está visualizando atualmente WooCommerce: Add New Tab @ Single Product Page

WooCommerce: Add New Tab @ Single Product Page

  • Autor do post:
  • Categoria do post:Woocommerce
  • Tempo de leitura:5 minutos de leitura

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.


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’ ); ” ); }

Was this article helpful?


Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete “?>” if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files – if you need more guidance, please take a look at my free video tutorial “Where to Place WooCommerce Customization?”

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance 🙂

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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

Post navigation