Sort Reviews/Ratings by date on Woocommerce

I would like to change the order the reviews are being displayed on the single product page, currently it’s in Ascending order and we need it in Descending order.(Most recent to oldest) I’ve looked at the review.php file but it appear to not have a sort order. If you could point me in the right direction or give me an alternative solution i’d greatly appreciate it.

Thanks.

Answer

WooCommerce questions are off-topic, but since reviews are merely comments, I think we can make the case that ordering comments is still on-topic.

wp_list_comments() has a reverse_top_level parameter that will set the most recent comment first and then go backwards. Assuming it works like I think it should then you’d want to add this parameter to WooCommerce’s wp_list_comments().

Conveniently, a filter is available for the wp_list_comments() args that are used in the single-product-reviews.php template.

<ol class="commentlist">
    <?php wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); ?>
</ol>

So, then to filter in new args, the process of filtering is the same as usual:

function wpa_filter_list_comments($args){
  $args['reverse_top_level'] = true;
  return $args;
}
add_filter( 'woocommerce_product_review_list_args', 'wpa_filter_list_comments' );

Attribution
Source : Link , Question Author : C Sanchez , Answer Author : helgatheviking

Leave a Comment