Conditional Woocommerce Product Tabs

The WooCommerce product tabs are pretty powerful once you understand the hooks and actions. See Modifying or Removing Product Tabs in Woocommerce.
That post discusses how to modify or hide product tabs in WooCommerce. This post describes how you can conditionally show them. For example, only show a tab if isn’t empty.

Here we are adding an Ingredients tab. Ingredients are stored in Post Meta as ingredients.

So initially, we return that custom post meta, if it exists, we can create a product tab.

add_filter( 'woocommerce_product_tabs', 'woo_new_product_tab' );
function woo_new_product_tab( $tabs ) {
global $post, $product;
$ingredients = get_post_meta($post->ID, 'ingredients', TRUE);
// Adds the new tab
if (!empty($ingredients)) {
$tabs['test_tab'] = array(
'title' => __( 'Ingredients', 'woocommerce' ),
'priority' => 50,
'callback' => 'woo_new_product_tab_content'
);
}return $tabs;
}
function woo_new_product_tab_content() {
global $post, $product;
$ingredients = get_post_meta($post->ID, 'ingredients', TRUE);
echo($ingredients);
}

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

2 thoughts on “Conditional Woocommerce Product Tabs”

  1. Fabulous share, thank you very much indeed you saved me a good bit of thinking time, exactly what I needed.

Leave a Reply

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