Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I want to display a total number of customer reviews on the homepage, I have tried this method:

<?php
  $args = array(
    'status'   => 'approve',
    'post_status' => 'publish',
    'post_type'   => 'product'
  );
  $comments_query = new WP_Comment_Query;
  $comments = $comments_query->query( $args );
  $count = get_comment_count($comments);
?>

<span class="total_reviews">
  <?php echo $count['approved'] . ' reviews' ?>
</span>

But isn't working like I want! for example, I have 4 reviews comments and this code show only (1 reviews) instead of (4 reviews).

About the average, I don't have any idea of how it's work on the homepage, I just know how to implement this on a single product page by using this code below:

$average = $product->get_average_rating();

But, this code is only for a single product average ratings, not a global average of all reviews as I would like.

Any help is appreciated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
436 views
Welcome To Ask or Share your Answers For Others

1 Answer

Update (Avoiding an error on the last function when there is no reviews yet)

Below you will find 4 custom functions that will give you:

  1. The total products reviews count
  2. The products ratings count data, that will be used in:
    • The products count by rating output html
    • The products rating average output html

THE FUNCTIONS CODE:

function get_total_reviews_count(){
    return get_comments(array(
        'status'   => 'approve',
        'post_status' => 'publish',
        'post_type'   => 'product',
        'count' => true
    ));
}

function get_products_ratings(){
    global $wpdb;

    return $wpdb->get_results("
        SELECT t.slug, tt.count
        FROM {$wpdb->prefix}terms as t
        JOIN {$wpdb->prefix}term_taxonomy as tt ON tt.term_id = t.term_id
        WHERE t.slug LIKE 'rated-%' AND tt.taxonomy LIKE 'product_visibility'
        ORDER BY t.slug
    ");
}

function products_count_by_rating_html(){
    $star = 1;
    $html = '';
    foreach( get_products_ratings() as $values ){
        $star_text = '<strong>'.$star.' '._n('Star', 'Stars', $star, 'woocommerce').'<strong>: ';
        $html .= '<li class="'.$values->slug.'">'.$star_text.$values->count.'</li>';
        $star++;
    }
    return '<ul class="products-rating">'.$html.'</ul>';
}

function products_rating_average_html(){
    $stars = 1;
    $average = 0;
    $total_count = 0;
    if( sizeof(get_products_ratings()) > 0 ) :
        foreach( get_products_ratings() as $values ){
            $average += $stars * $values->count;
            $total_count += $values->count;
            $stars++;
        }
        return '<p class="rating-average">'.round($average / $total_count, 1).' / 5 '. __('Stars average').'</p>';
    else :
        return '<p class="rating-average">'. __('No reviews yet', 'woocommerce').'</p>';
    endif;
}

Code goes in function.php file of your active child theme (or active theme). Tested and works.

USAGE

  1. Total customers reviews:

    echo '<p>'.__('Total reviews','woocommerce').': '.get_total_reviews_count().'</p>';
    
  2. The products count by ratings list:

    echo products_count_by_rating_html();
    
  3. The products rating average:

    echo products_rating_average_html();
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...