空时隐藏 WooCommerce 产品简短描述自定义选项卡

Hide WooCommerce product short description custom tab when its empty

我正在使用 在自定义产品选项卡中移动 Woocommerce 产品简短描述。

不幸的是,当没有简短描述时,我不知道如何取消设置自定义选项卡。

如何在自定义选项卡为空时隐藏 WooCommerce 产品简短描述?

如果产品简短描述为空,以下将隐藏此自定义选项卡:

// 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.;
    }
}

代码进入您的活动子主题(或活动主题)的 functions.php 文件。已测试并有效。