Display blank(0) star ratings in the Woocommerce s

2019-01-28 17:10发布

问题:

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.

回答1:

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.