在 Woocommerce 单个产品页面中显示空白 (0) 星级 [英] Display blank(0) star ratings in the Woocommerce single product pages

查看:32
本文介绍了在 Woocommerce 单个产品页面中显示空白 (0) 星级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 WooCommerce 产品页面中,星级评分仅在有人在标题下方提交评论时可见.但我想在没有可用评论时显示空白 [0] 评分.

解决方案

以下代码将处理单个产品页面上的评分计数显示(没有评论时显示零):

add_action('woocommerce_single_product_summary', 'change_single_product_ratings', 2 );函数 change_single_product_ratings(){remove_action('woocommerce_single_product_summary','woocommerce_template_single_rating', 10);add_action('woocommerce_single_product_summary','wc_single_product_ratings', 10);}函数 wc_single_product_ratings(){全球$产品;$rating_count = $product->get_rating_count();如果 ( $rating_count >= 0 ) {$review_count = $product->get_review_count();$average = $product->get_average_rating();$count_html = '

'.array_sum($product->get_rating_counts()) .'</div>';?><div class="woocommerce-product-rating"><div class="container-rating"><div class="star-rating"><?php echo wc_get_rating_html( $average, $rating_count );?></div><?php echo $count_html ;?><?php if ( comments_open() ) : ?><a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s 客户评论', '%s 客户评论', $review_count, 'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a><?php endif ?></div></div><?php}}

代码位于活动子主题(或活动主题)的 function.php 文件中.经测试有效.

In my WooCommerce product page, the star ratings only visible when someone submits a review at the below of the title. But I want to display a blank[0] ratings when there are no reviews available.

解决方案

The following code will handle rating count display on single product pages (displaying Zero when there is no reviews):

add_action('woocommerce_single_product_summary', 'change_single_product_ratings', 2 );
function change_single_product_ratings(){
    remove_action('woocommerce_single_product_summary','woocommerce_template_single_rating', 10 );
    add_action('woocommerce_single_product_summary','wc_single_product_ratings', 10 );
}

function wc_single_product_ratings(){
    global $product;

    $rating_count = $product->get_rating_count();

    if ( $rating_count >= 0 ) {
        $review_count = $product->get_review_count();
        $average      = $product->get_average_rating();
        $count_html   = '<div class="count-rating">' . array_sum($product->get_rating_counts()) . '</div>';
        ?>
        <div class="woocommerce-product-rating">
            <div class="container-rating"><div class="star-rating">
            <?php echo wc_get_rating_html( $average, $rating_count ); ?>
            </div><?php echo  $count_html ; ?>
            <?php if ( comments_open() ) : ?><a href="#reviews" class="woocommerce-review-link" rel="nofollow">(<?php printf( _n( '%s customer review', '%s customer reviews', $review_count, 'woocommerce' ), '<span class="count">' . esc_html( $review_count ) . '</span>' ); ?>)</a><?php endif ?>
        </div></div>
        <?php
    }
}

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

这篇关于在 Woocommerce 单个产品页面中显示空白 (0) 星级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆