2
votes

In Woocommerce, I am trying to display a report (HTML Table) for each product in something like:

enter image description here

Based on "Display total customers reviews and ratings average in WooCommerce" answer code, which gets the product average rating this way:

echo products_rating_average_html();

How to can I get the average rating for each product individually… for example if the product have 4 or 5 reviews it get the average rating for them (For every product)

I also would like to get the comment review for every product to put them in the table as well…

2

2 Answers

1
votes

This will give the avarage rating of given product id

$rating = get_post_meta( $product_id, '_wc_average_rating', true );

You can use this in loop of product this will give you a raw number of avrage rating without any html

For getting latest comment you can use below add this code in functions.php and then call this action where you want to display latest comment pass current product object as argument

function display_product_review($product_id) {

    $product = wc_get_product( $product_id );
    $comments = get_approved_comments( $product->id );

    $product_link = '/product/' . $product->post->post_name . "/#tab-reviews/";

    if ( !empty ($comments) ) {
        echo $comments[0]->comment_content . '<br><a href="'. $product_link . '">Read more reviews...</a>';
    } else {
        echo "There are no reviews for this product yet. Would you like to <a href='" . $product_link ."'>add your own review</a>?";       
    }
}

add_action('get_latest_review','display_product_review');

Calling of action

do_action('get_latest_review',$product);
1
votes

Have a look to this answer thread, where you'll see that you can use WC_Product methods like:

  • get_average_rating()
  • get_rating_counts()

Here is a complete example:

1) Get the products required related data (customizable):

$products_data = []; // Initializing

$products = wc_get_products( ['limit' => -1] ); // Get all WC_Product objects

// Loop through products -- Preparing data to be displayed
foreach ( $products as $product ) {
    $product_id = $product->get_id();

    // 1. Product data as product name …
    $products_data[$product_id]['name'] = $product->get_name();


    // 2. Average rating and rating count
    $products_data[$product_id]['rating'] = (float) $product->get_average_rating();
    $products_data[$product_id]['count']  = (int) $product->get_rating_count();

    // 3. Reviews -- Loop though product reviews
    foreach( get_approved_comments( $product_id ) as $review ) {
        if( $review->comment_type === 'review' ) {
            $products_data[$product_id]['reviews'][] = (object) [
                'author'    => $review->comment_author,
                'email'     => $review->comment_author_email,
                'date'      => strtotime($review->comment_date),
                'content'   => $review->comment_content,
            ];
        }
    }
}

2) Customizable display in a table:

echo '<table class="products-reviews-ratings"><tr>
    <th>'.__("ID").'</th>
    <th>'.__("Name").'</th>
    <th>'.__("Rating").'</th>
    <th>'.__("count").'</th>
    <th style="text-align:center">'.__("Reviews (author, date and content)").'</th>
</tr>';

foreach ($products_data as $product_id => $data){
    echo '<tr>
        <td>'.$product_id.'</td>
        <td>'.$data['name'].'</td>
        <td>'.$data['rating'].'</td>
        <td>'.$data['count'].'</td>
        <td style="text-align:center">';

    if( isset($data['reviews']) && $data['reviews'] > 0 ) {
        echo '<table>';

        // Loop through reviews
        foreach ($data['reviews'] as $review ){
            echo '<tr>
                <td><a href="mailto:'.$review->email.'">'.$review->author.'</a></td>
                <td>'.date( 'Y-m-d', $review->date ).'</td>
                <td>'.$review->content.'</td>
            </tr>';
        }
        echo '</table>';
    } else {
        _e('No reviews yet');
    }

    echo '</td></tr>';
}
echo '</table>';

You will get something like:

enter image description here