Hide WooCommerce product short description custom tab when its empty

Viewed 561

I'm using Move short description into tabs in Woocommerce single product pages to move the Woocommerce product short description in a custom product tab.

Unfortunately I can't figure out how to unset the custom tab when there is no short description present.

How to hide WooCommerce product short description from a custom tab when its empty?

1 Answers

The following will hide this custom tab if product short description is empty:

// Add short description as a new custom product tab
add_filter( 'woocommerce_product_tabs', 'add_custom_product_tab' );
function add_custom_product_tab( $tabs ) {
    global $post, $product;

    $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );

    if ( ! empty($short_description) ) {

        $tabs['short_description'] = array(
            'title'     => __( "What's in the box", "woocommerce" ),
            'priority'  => 200,
            'callback'  => 'short_description_tab_content'
        );
    }
    return $tabs;
}

// Custom product tab content
function short_description_tab_content() {
    global $post, $product;

    $short_description = apply_filters( 'woocommerce_short_description', $post->post_excerpt );

    if ( ! empty($short_description) ) {
        echo '<div class="woocommerce-product-details__short-description">' . $short_description . '</div>'; // WPCS: XSS ok.;
    }
}

Code goes in functions.php file of your active child theme (or active theme). Tested and works.

Related