WooCommerce Tutorials

How To Display Most Recent WooCommerce Product Reviews First

WooCommerce is now regarded as one of the most popular eCommerce platforms available with well over a million users, not bad for what is effectively a WordPress plugin right?

I have used a number of different platforms in my career building eCommerce stores, but WooCommerce is still my go-to option every time. In my eyes, there simply is no other option out there as flexible and open to modification and this article is going to make use of that very flexibility to alter the way in which your WooCommerce reviews are presented to customers.


Should You Worry About Product Reviews?

Let me answer that with another question. When you are browsing online to make a purchase and find the product you are after in multiple stores at the same price, how do you make your final decision? I bet a majority of you would admit to using reviews to make your final decision.

You have to keep in mind that product reviews say much more to a new potential customer than just whether someone liked or disliked the product. Reviews also prove that people are purchasing and actively engaging with your store, and with so many new online stores being added to the web daily, customers need as many reasons as possible to feel safe in purchasing from yours.

There are many more benefits to receiving product reviews on your website, and I cover these in my article explaining how to make the most of your WooCommerce product reviews which I urge you to check out as well.


WooCommerce Review Settings

WooCommerce offers a number of settings within the admin panel to allow you to set how far reviews are nested, or whether people need to register before leaving their comment, but unfortunately, it does not offer the option to decide whether your most recent product reviews are shown first or last.

By default, WooCommerce shows the oldest reviews first from which you can work your way through the pagination to find the most recent. I personally always want to see the latest reviews first so I can check the date to feel the comfort that others have shopped recently with the store as well.


How To Display Your Newest WooCommerce Product Reviews First

Let’s start by taking a look as to how we can achieve this. Yes, there are many plugins available that will give you this functionality, but do you want yet another plugin installed on your site for such a simple modification? Probably not.

If you dig into the WooCommerce plugin, within the templates folder you will find a file named single-product-reviews.php, and guess what, this is where those reviews, or comments as they are referred to generically in WordPress land, are handled to be displayed. More specifically, if you take a look around line 44 you will spot the following code.

<?php 
wp_list_comments( apply_filters( 'woocommerce_product_review_list_args', array( 'callback' => 'woocommerce_comments' ) ) ); 
?>

This block might blow your mind a little if you are new to the inner workings of WordPress, but if there is one thing we should be pleased to see here, it is the fact that the review arguments are passed through a filter labeled ‘woocommerce_product_review_list_args‘, and this means we can get our hands on them and make alterations before they are handed to the page to be displayed, perfect.

If you are just starting off on your road to learning the depths of WordPress development, I would fully recommend you begin by learning all about Actions and Filters, they can be tough to get your head around at first, but will end up saving you lots of stress down the line. Time well spent in my opinion.

Back to business, so we have a filter name, and we know the comment arguments are being passed through that filter. Now, all we need to do is write up a simple function within the infamous functions.php file of our current theme.

<?php
// Reverse The Order Of WooCommerce Product Reviews
function wpcodetips_reverse_reviews($reviewArguments){
    // Interject the reverse top level argument and set it to true
    $reviewArguments['reverse_top_level'] = true;
    // Return the areguments back again after alteration
    return $reviewArguments;
}
add_filter('woocommerce_product_review_list_args','wpcodetips_reverse_reviews');
?>

I have commented the code as much as I can, but the reality is there is nothing too complicated going on here. Our function makes use of the filter mentioned earlier, alters the boolean value of the ‘reverse_top_level‘ argument, and then returns the entire argument array back again. Take out the comments and you have just five lines of code to get the job done, no plugin required.


Overriding The Template File

The slightly more experienced developers of you might rightly point out that there is another way this modification can be achieved. At the top of the single-product-reviews.php file, you will notice the following comment.

This template can be overridden by copying it to yourtheme/woocommerce/single-product-reviews.php

This means you have the option to replicate the file within your own theme of which the contents would override that of the original file. This is, of course, another perfectly reasonable way to alter the workings without causing problems when WooCommerce updates come along, but why would you want to go through the hassle of copying 145 lines of code when you could just add 5?


Conclusion

Add this code to your themes function file and let me know how it worked for you in the comments below! Ok, WooCommerce might not offer as many options within the admin panel as other platforms, but it almost always makes up for it by offering a range of hooks to let you achieve the same results yourself!

If you are struggling to apply the change, or have any other questions, then please get in touch and we will try our best to help!


Buy Me a Coffee?Buy Me a Coffee?
Gary

Hi, my name is Gary, I hope to share some of my real-world programming experiences and code solutions to help you develop your Wordpress and WooCommerce skills.

Leave a Reply

Your email address will not be published. Required fields are marked *