H
H
hypero2021-07-02 21:24:42
WordPress
hypero, 2021-07-02 21:24:42

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

1 answer(s)
V
Vladimir Druzhaev, 2021-07-07
@OtshelnikFm

$prod_average = $prod->get_average_rating();
is overwritten in the cycle. As a result, $prod_averageyour 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 question

Ask a Question

731 491 924 answers to any question