Sunday 24 September 2017

How to get the Average rating in WooCommmerce shop page?

Sometime there may a requirement where client can ask to show the average ratings for WooCommrce products on the shop page.

This can be achieve by making some customisation in the WooCommerce by following the below steps:
1) Create a folder name "woocommerce" in your active theme.
2) Now we need a file name "rating.php" which you need to copy from WooCommerce plugin (wp-content/plugins/woocommerce/templates/loop/rating.php); copy this file.
3) Create a loop folder inside the woocommerce folder in theme (Point number 1), paste in that folder and open this file in code editor.
4) Before starting to make any changes in file, go to your admin panel, click on WooCommerce on left side menu then click on "Status" (woocommerce>status) and at very bottom of the page there would be a section name "Templates", this section will show all files which are/which we have moved to theme woocommerce folder. please not if any file you have copy to woocommerce folder in theme is not showing there it means either you have included the non template files or have copy the file in wrong folder (which is not as per the guidelines of the WooCommerce).
5) Once file is verified in the status; then use the following code to show the "Average rating" in the same file.

echo $product->get_average_rating();

Save the file and go to your shop page.

Now you can style the same as per your own preference/theme design.

Version at the time of demonstration:
WordPress 4.8.2
WooCommerce 3.1.2

Load disqus comments