Show star rating in product item loop in WooCommerce

Some Templates hide the product review statics from the shop page. To display the review in brief on the product page here is the snippet that can be used. In the below image you can see there is no such star rating.

Before applying snippet

Now let’s add the star rating to the products. Add the following snippet to your function.php file in your child theme.

function wps_add_star_rating() {

    if (is_shop()) {

         global $woocommerce, $product;

         // getting an average rating

         $average = $product->get_average_rating();

         // printing the rating star
 
         echo '<div class="product-start-rating"><span style="width:'.( ( 
         $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" 
         class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' 
         ).'</span></div>';   

    }

}

add_action('woocommerce_after_shop_loop_item', 'wps_add_star_rating' );

Here in the above function, we are checking whether the page is a shop page or not if Yes, then we are getting the product average star rating in the $average variable.
At last, we are calculating the rating and printing it.

After applying snippet

Edit WooCommerce Products from Frontend

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top