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?
Leave a Reply
You must be logged in to post a comment.
There’s actually a much more terse way to handle this. Just use the built-in functions that Woocommerce has built out:
I’ve confirmed that this works for me.
You can put this into your themes functions.php file:
Note that you may need to change
woocommerce_after_shop_loop_item
to a different hook depending on your design and where exactly you want the stars to show up.This page lists WooCommerce action hooks: http://wcdocs.woothemes.com/codex/extending/hooks/ . I don’t see any hooks associated with the thumbnail specifically, but you may try
woocommerce_after_shop_loop_item_title
orwoocommerce_after_shop_loop_item
.(This function is essentially copied from WooCommerce’s single-product-reviews.php)
As of WooCommerce 3.0+ this is the correct code:
The get_rating_html() method on the product object has been deprecated.
More information here: https://docs.woocommerce.com/wc-apidocs/function-wc_get_rating_html.html
With those functions (or the shorter one below, which outputs the same HTML), you manage to output the rating in numbers, reusing Woocommerce’s functions completely:
Then you need to style it to show the stars. For a rating of 3 out of 5 stars, Woothemes does it like this (the trick is in the width and :before of the corresponding CSS):
HTML (output by Woocommerce):
And the CSS (there may be some redundant lines in here):
Hope that helps!