1

How do I display a rating at all times, even if it is empty? This is for woocommerce, meant for product boxes like popular products and new products, doesn't have to show up on single product pages.

1
  • @TemaniAfif That's the opposite of what he's asking. Commented Nov 29, 2017 at 11:12

2 Answers 2

1

Add the following code to your functions.php

add_action('woocommerce_after_shop_loop_item_title','change_loop_ratings_location', 2 );
function change_loop_ratings_location(){
remove_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating', 5 );
add_action('woocommerce_after_shop_loop_item_title','woocommerce_template_loop_rating', 15 );
}

And then after add the following lines also to get the rating count

add_filter( 'woocommerce_product_get_rating_html', 'loop_product_get_rating_html', 20, 3 );
function loop_product_get_rating_html( $html, $rating, $count ){
    if ( 0 < $rating && ! is_product() ) {
        global $product;
        $rating_cnt = array_sum($product->get_rating_counts());
        $count_html = ' <div class="count-rating">' . $rating_cnt .'</div>';

        $html       = '<div class="container-rating"><div class="star-rating">';
        $html      .= wc_get_star_rating_html( $rating, $count );
        $html      .= '</div>' . $count_html . '</div>';
    }
    return $html;
}
0

try this in your functions.php:

add_filter('woocommerce_product_get_rating_html',function ( $html, $rating, $count){
    $label = sprintf( __( 'Rated %s out of 5', 'woocommerce' ), $rating );
    $html  ='<div class="star-rating" role="img" aria-label="' . esc_attr( $label ) . '">' . wc_get_star_rating_html( $rating, $count ) . '</div>';
    return $html;
},9999,3);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.