【问题标题】:Removing the (0) on the WooCommerce Reviews Tab删除 WooCommerce 评论选项卡上的 (0)
【发布时间】:2016-02-02 22:03:27
【问题描述】:

当没有评论时,我已成功删除评论标签标题上的 (0)。在市场营销中 - 最好不要显示产品有 0 条评论。这是我放置在 WooCommerce 插件文件 wc-template-function.php 中的子主题的 functions.php 文件中的代码:

if ( ! function_exists( 'woocommerce_default_product_tabs' ) ) {

/**
 * Add default product tabs to product pages.
 *
 * @param array $tabs
 * @return array
 */
function woocommerce_default_product_tabs( $tabs = array() ) {
    global $product, $post;

    // Description tab - shows product content
    if ( $post->post_content ) {
        $tabs['description'] = array(
            'title'    => __( 'Description', 'woocommerce' ),
            'priority' => 10,
            'callback' => 'woocommerce_product_description_tab'
        );
    }

    // Additional information tab - shows attributes
    if ( $product && ( $product->has_attributes() || ( $product->enable_dimensions_display() && ( $product->has_dimensions() || $product->has_weight() ) ) ) ) {
        $tabs['additional_information'] = array(
            'title'    => __( 'Additional Information', 'woocommerce' ),
            'priority' => 20,
            'callback' => 'woocommerce_product_additional_information_tab'
        );
}

    // Reviews tab - shows comments
    if ( comments_open() ) {
    $check_product_review_count = $product->get_review_count();
    if ( $check_product_review_count == 0 ) {
        $tabs['reviews'] = array(
            'title'    => sprintf( __( 'Reviews', 'woocommerce' ) ),
            'priority' => 30,
            'callback' => 'comments_template'
        );
        }
        else {
        $tabs['reviews'] = array(
            'title'    => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ), $product->get_review_count() ),
            'priority' => 30,
            'callback' => 'comments_template'
        );
        }
    }

    return $tabs;
}
} 

我的问题是 - 这是在不更改 woocommerce 核心文件的情况下修改它的最有效方法吗?函数“woocommerce_default_product_tabs”是一个可插入函数,但似乎我可以以某种方式使用过滤器,而不是将整个函数复制到我的子主题中并从那里进行编辑。我只需要得到这行代码:

title'    => sprintf( __( 'Reviews (%d)', 'woocommerce', $product->get_review_count() ),

并添加一个 if 语句来检查是否没有 cmets 来更改上面的行,就像上面的行一样:

title'    => sprintf( __( 'Reviews', 'woocommerce' ),

【问题讨论】:

    标签: php wordpress function woocommerce review


    【解决方案1】:

    这很容易。您可以更改任何选项卡的标题:

    add_filter( 'woocommerce_product_tabs', 'wp_woo_rename_reviews_tab', 98);
    function wp_woo_rename_reviews_tab($tabs) {
        global $product;
        $check_product_review_count = $product->get_review_count();
        if ( $check_product_review_count == 0 ) {
            $tabs['reviews']['title'] = 'Reviews';
        } else {
            $tabs['reviews']['title'] = 'Reviews('.$check_product_review_count.')';
        }
        return $tabs;
    }
    

    【讨论】:

    • 是的 - 做到了。感谢您的帮助,亚历克斯。
    • 感谢剪纸。我想要如果评论 0 然后隐藏总数。我的意思是评论 还没有评论。会隐藏。知道如何
    • @pagol 如果因为评论选项卡被隐藏而无法进入评论选项卡,人们将如何发布评论?
    猜你喜欢
    • 2016-01-30
    • 2020-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-21
    • 2015-01-01
    • 2012-03-09
    • 2015-07-11
    相关资源
    最近更新 更多