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

时间:2020-08-06 08:21:33

标签: php wordpress woocommerce tabs hook-woocommerce

我正在使用 Move short description into tabs in Woocommerce single product pages 在自定义产品标签中移动Woocommerce产品的简短描述。

很遗憾,在没有简短说明的情况下,我无法弄清楚如何取消自定义标签。

当WooCommerce产品为空时,如何从自定义标签中隐藏其简短说明?

1 个答案:

答案 0 :(得分:0)

如果产品简短描述为空,则以下内容将隐藏此自定义标签:

// 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文件中。经过测试,可以正常工作。