Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 546
kbsas14 Avatar asked Jan 09 '13 01:01

kbsas14


1 Answers

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.

like image 114
Martyn Chamberlin Avatar answered Oct 25 '22 09:10

Martyn Chamberlin