If you do have brand evangelists praising your products, you should make sure their reviews are visible. If you have products that are hard to justify, some social affirmation that other people enjoy the product will be a huge benefit. If that's the case, you might want to highlight a product review in the product description to make sure every user sees it.
There aren't any plugins that do this. We'll have to custom-code this by ourselves. Unlike many of the code snippets I've shared, this one is a bit lengthier and is broken up into several pieces. Let's take a look at the following steps:
functions.php
file or a custom WooCommerce plugin.add_action( 'woocommerce_single_product_summary', 'woocommerce_cookbook_single_review', 25 );
function woocommerce_cookbook_single_review() { // get all of the comments $args = array ('post_type' => 'product'), $comments = get_comments( $args ); // get the best comment $best_comment = woocommerce_cookbook_get_best_comment( $comments); // if there is a best comment then print it if ( $best_comment > 0 ) { woocommerce_cookbook_print_best_comment( $comments[$best_comment] ); } }
This code gets all of the comments for a product and then sends them to a function that picks the best comment. If this function gets a best comment, it sends the comment to a different function that prints it. If you ran this code right now, it would break the page. We still need to write those two extra functions. In our case, these two functions will be woocommerce_cookbook_get_best_comment
and woocommerce_cookbook_print_best_comment
.
function woocommerce_cookbook_get_best_comment( $comments ) { $best_comment = 0; // loop through each comment to find the best foreach( $comments as $key => $comment ) { // get the rating $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ); // save the rating in the comment $comment->rating = $rating; // if the rating is higher, it's approved, and there are at least 10 characters, save it if ( $rating > 0 && $rating > $comments[$best_comment]->rating && '1' == $comment->comment_approved && 10 < strlen( $comment->comment_content ) ) { // save the array key of the comment $best_comment = $key; } } return $best_comment; }
This function looks for the best comment. It will go through the entire list of comments and look for the highest-rated review that is approved and has at least 10 characters. It then returns the index of that comment to the original function.
If we get a best comment, we now need to write one last function to display it.
function woocommerce_cookbook_print_best_comment( $comment ) { // print out the best comment and some very basic styles ?> <p>Here's what people are saying about this product:</p> <blockquote class='comment-text'> <?php echo apply_filters( 'comment_text', $comment->comment_content ); ?> </blockquote> <style> .comment-text{ font-style: italic; } </style> <?php }
This function prints out any comment that gets passed in as well as some very basic styles to visually separate it from other content.
If you take a look at the frontend of your site, you should see the review in the product description.
There are many ways to take this even further. You could change the code to show two comments, you could change the display on the frontend to make it stand out even more, or you could make the experience nicer by linking the comment in the top to the comments in the bottom. When you write custom code, you can do just about anything.