I’m building a WooCommerce website that has a custom review system. I want to show the same reviews for multiple products that are variations but have their own product page.
Still I want to be able to sort products based on their rating, so I want to programmatically update ‘_wc_average_rating’ whenever a custom review is placed. Also the rich data markup is using these fields to populate the rich data markup.
I’m using the following code to update the meta values.
update_post_meta( 13566, '_wc_review_count', 4);
update_post_meta( 13566, '_wc_rating_count', 4);
update_post_meta( 13566, '_wc_average_rating', 5.00);
For the review and rating count it is working fine, but for the average rating it’s not working.
I tried different things, like:
update_post_meta( 13566, '_wc_average_rating', 5.00);
update_post_meta( 13566, '_wc_average_rating', 5);
update_post_meta( 13566, '_wc_average_rating', '5.00');
How can I update the _wc_average_rating meta field? Is there a function to do this?
Thanks so much!
2
Answers
My website is running WPML, and that plugin uses it's own 2 meta fields to sync rating/reviews across different languages. The meta fields are:
So to update the rating in a WPML website, you need to update all these fields:
I’ve tried the following code to verify your approach, and on my site its totally working. My average rating for my product 249 gets changed to 2:
If you check this image – my first rating set the average rating to 5 and I changed it with my code to 2:
So I really don’t know what’s not working on your site. I just can confirm that updating the average rating is possible the way I showed you.
Maybe you can show me the place where you execute your code. Sometimes this may be the problem.