Recently a client asked me about “How to display WooCommerce reviews on the homepage. He wanted to get all the different product reviews out of the system and display the 6 reviews on the home page with the title and a link to the product that the review was for. In this post I will show you How to display WooCommerce reviews on any page of your choice. In fact I will also show you how to generate a shortcode for it because I have already done this work and I can post the code for the community and save someone else a bunch of time.
Personally, I think its a great idea to display product Reviews on landing pages for marketing purposes. Its easier to sell a product if it has already established the credentials in the eyes of a customer. Please keep in mind that you will need a “Child Theme” if you are working with a theme that you will want to update in the future. The reason is we want to preserve our code and keep the theme in use free of any customization work. I assume for the sake of this post that you are familiar with what a Child Theme Is.
How to display WooCommerce reviews
Woocommerce Product Reviews are basically stored with the comment system in the WordPress System. Internally they are stored as Post Comments. WooCommerce also creates a taxonomy for the post_type Product. In my example we will retrieve any product reviews. To do that I will use the WP_COMMENT_QUERY which was introduced and available in WordPress 3.6.X release (I believe, I might be wrong).
The First Step
The first step is to create a php function in functions.php. Again a friendly reminder that you should be using a Child Theme. Always back up before making modifications because syntax errors in functions.php are fatal. In the function you can use code similar to the one pasted below to retrieve comments from the system that apply to products
$count = 0;
$html_r = "";
$args = array(
'post_type' => 'product'
$comments_query = new WP_Comment_Query;
$comments = $comments_query->query( $args );
foreach($comments as $comment) :
$title = ''.get_the_title( $comment->comment_post_ID ).'';
$html_r = $html_r. "" .$title."";
$html_r = $html_r. "" .$comment->comment_content."";
$html_r = $html_r."Posted By".$comment->comment_author." On ".$comment->comment_date. "";
The code here is pretty self explanatory. First we create a WP_COMMENT_QUERY instance and pass in the filter for post_type to be products. Then based on the return values we simply loop through each comment, list the product and the link that the review was for and add some formatting.
Note: This is pretty basic formatting and you will need to refine the code to make sure that it is formatted to your requirements.
Hope you get the idea here.
Creating A Shortcode For our function
You can simply add the line below after the end of your function to create a shortcode for it:
This means that you can put [woo_reviews] on any page and it will work and display ALL your product reviews on your page. Now this will need modification to pass in the parameters like the product ID or the number of comments to retrieve but I will leave that up to you. The conclusion is that WP_COMMENTS_QUERY and Woocommerce is Awesome! I love this kind of stuff and thank my client to have given me the opportunity to learn it. This post will be helpful for me down the road and I might release a plugin for this in the near future.
IDesignSmart is a WordPress development and design company based in Atlanta, GA. We offer WordPress Solutions and WordPress coaching at an affordable rate.