How do you add the star ratings for products in woocommerce?

kbsas14 picture kbsas14 · Jan 9, 2013 · Viewed 68.2k times · Source

I have a woocommerce store and I want to add star ratings to each of the products when you see their thumbnails. I already have the stars in the big product view but I want them to display below each thumbnail like in most ecommerce stores like timberland.com. I know i can use css to disable items from view but not add them. Any thoughts?

Answer

Martyn Chamberlin picture Martyn Chamberlin · Dec 27, 2013

There's actually a much more terse way to handle this. Just use the built-in functions that Woocommerce has built out:

add_action('woocommerce_after_shop_loop_item', 'get_star_rating' );
function get_star_rating()
{
    global $woocommerce, $product;
    $average = $product->get_average_rating();

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

I've confirmed that this works for me.