Answer the question
In order to leave comments, you need to log in
Display the overall rating of products?
Hello.
I'm trying to display the overall average rating of all products displayed through the cycle.
As a result, the average rating of one product is displayed.
Who can help?
<?php
$products = wc_get_products( ['post_type'=>'product', 'post_parent'=>$post->ID, 'post_type'=>'product'] );
foreach( $products as $prod ){
$prod_rating_count = $prod->get_rating_count();
$prod_review_count = $prod->get_review_count();
$prod_average = $prod->get_average_rating();
}
echo wc_get_rating_html( $prod_average );
?>
Answer the question
In order to leave comments, you need to log in
$prod_average = $prod->get_average_rating();
is overwritten in the cycle. As a result, $prod_average
your variable contains the last value from the loop.
What do you have there - or sum in a loop, then divide by the number of foreach iterations or something else ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question