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

One thought on “Conditional Woocommerce Product Tabs

  1. Thanks a lot! Was looking for this!

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>