woocommerce has a shortcode [top_rated_products] that you can use it in a page.
that displays the top rated products (review rating in each product) shorted by their average.
The problem is, that it is not taking account how many reviews are in each product.
In other words what i want to do is shorting like the the following
product 55 – rating 5 – number of reviews 8
product 38 – rating 5 – number of reviews 5
product 22 – rating 5 – number of reviews 2
at the moment it is displaying it, ordered by AVG rating and post date (if i have understood it well). So it looks like this
product 38 – rating 5 – number of reviews 5
product 55 – rating 5 – number of reviews 8
product 22 – rating 5 – number of reviews 2
I found out (if i am right) that the ordering is taking place in in
wp-content/plugins/woocommerce/includes/class-wc-query.php
under the function
public function order_by_rating_post_clauses
I have added the following code but I cannot see an improvement. Any ideas what I am doing wrong?
thank you in advance
public function order_by_rating_post_clauses( $args ) {
global $wpdb;
$args['fields'] .= ", AVG( $wpdb->commentmeta.meta_value ) as average_rating ";
//***** ADDED BY ME ************
$args['fields'] .= ", COUNT( $wpdb->commentmeta.meta_value ) as count_rating ";
//***** ADDED BY ME ENDS *******
$args['where'] .= " AND ( $wpdb->commentmeta.meta_key = 'rating' OR $wpdb->commentmeta.meta_key IS null ) ";
$args['join'] .= "
LEFT OUTER JOIN $wpdb->comments ON($wpdb->posts.ID = $wpdb->comments.comment_post_ID)
LEFT JOIN $wpdb->commentmeta ON($wpdb->comments.comment_ID = $wpdb->commentmeta.comment_id)
";
//****** ORIGINAL LINE ***********
// $args['orderby'] = "average_rating DESC, $wpdb->posts.post_date DESC";
//****** REPLACED BY *************
$args['orderby'] = "average_rating DESC, count_rating, $wpdb->posts.post_date DESC";
$args['groupby'] = "$wpdb->posts.ID";
return $args;
}