WooCommerce: Display Product Reviews @ Custom Page (Shortcode)

WooCommerce product reviews shows by default in the “Reviews” tab in the single product page. But what if, like me, you’re using custom sales pages and need to show such reviews elsewhere – by using a shortcode?

I’ve spent some time doing this for two Business Bloomer pages, the contact page (beside the request a quote form) and the Business Bloomer Club sales page (just above the pricing table), so I thought it would have been great sharing the snippet with you. Enjoy!

Here’s my custom “WooCommerce product reviews shortcode” output on a test website homepage.

PHP Snippet: WooCommerce Product Reviews Shortcode

Once the snippet below is added to your functions.php, simply use shortcode [[product_reviews id=”123″]] anywhere you like. Please note “id” is the ID of the product for which you want to output customer reviews.

/**
 * @snippet       WooCommerce Product Reviews Shortcode
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @testedwith    WooCommerce 3.9
 * @community     https://businessbloomer.com/club/
 */

add_shortcode( 'product_reviews', 'bbloomer_product_reviews_shortcode' );

function bbloomer_product_reviews_shortcode( $atts ) {
	
	if ( empty( $atts ) ) return '';

	if ( ! isset( $atts['id'] ) ) return '';
		
	$comments = get_comments( 'post_id=' . $atts['id'] );
	
	if ( ! $comments ) return '';
	
	$html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
	
	foreach ( $comments as $comment ) {	
		$rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
		$html .= '<li class="review">';
		$html .= get_avatar( $comment, '60' );
		$html .= '<div class="comment-text">';
		if ( $rating ) $html .= wc_get_rating_html( $rating );
		$html .= '<p class="meta"><strong class="woocommerce-review__author">';
		$html .= get_comment_author( $comment );
		$html .= '</strong></p>';
		$html .= '<div class="description">';
		$html .= $comment->comment_content;
		$html .= '</div></div>';
		$html .= '</li>';
	}
	
	$html .= '</ol></div></div>';
	
	return $html;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Display All Products Purchased by User
    When a WooCommerce customer is logged in, you might want to show them the list of previously purchased products (maybe in a custom “My Account” tab). This is helpful when customers tend to buy the same products over and over again, and therefore you can help them “order again” without having them to search the […]
  • WooCommerce: Display Out of Stock Products (Shortcode)
    A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all! You can use this shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof. So let’s see (1) […]
  • WooCommerce: Recently Viewed Products (Shortcode)
    Currently, you can use a widget or a block to have the user see the list of products they recently viewed. But for now, let’s create our own shortcode… and let’s take advantage of the existing Business Bloomer ClubRated 5.00 out of 5 $9.00 – $599.00 /once Whether youโ€™re only starting with WooCommerce or youโ€™re […]
  • WooCommerce: Exclude Category from ‘product_categories’ Shortcode
    Sometimes solutions are very simple, and you don’t need rocket science to fix your issue! A client of mine needed to hide a category from the Product Categories Shortcode ( BloomerArmada (1) WooCommerce Mini-Plugins (27) ); in fact, there is no parameter that allows you to “exclude” a given product category such as “uncategorized” or […]
  • WooCommerce: Exclude Category from ‘products’ Shortcode
    The WooCommerce Business Bloomer ClubRated 5.00 out of 5 $9.00 – $599.00 /once Whether youโ€™re only starting with WooCommerce or youโ€™re an experienced WordPress developer, weโ€™ve got you covered. Enjoy day-to-day WooCommerce support, attend a Woo masterclass, view the member directory… joining the Club is really a no-brainer. Share your feedback View Plugin WooCommerce Toggle […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

52 thoughts on “WooCommerce: Display Product Reviews @ Custom Page (Shortcode)

  1. Amazing work Rodolfo. If I would like to add the link so users can leave a review, how do I do it?

    1. Hello Ricky, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. I know this is older, but this tweak adds the stars and formatting very nicely. It’s also picking up comments from a recent blog post.(?)

    1. Weird! Did you use the correct product ID inside the shortcode?

  3. Hey Rodolfo,
    Do you happen to have the addl css to turn the stars gold? #FFD700

      1. In the additional CSS add

        .star-rating{color:#FFD700}

  4. Thanks for this even i was looking for from last 6 months and finally i found this amazing blog and it worked for me but i have a query on the same, please go through this page as you can see the same results but i want product name along with reviews . is it possible?

    1. Thanks for your comment Piyush! You need to specify the product ID inside the shortcode, so why can’t you add the product name by “hand” just above the shortcode output?

  5. Hi, Nice snippet. Is ther a way to get product id dynamcally? I need to add this shortcode into a product template, so I can’t specify the prdocut ID.

    1. Hi Jessica, for sure!

      Try replace

      if ( empty( $atts ) ) return '';
      if ( ! isset( $atts['id'] ) ) return '';
      

      with

      if ( ! is_product() ) return '';
      $atts['id'] = get_queried_object_id();
      
  6. What about “Add a Review” button for that ID? Can you help me?

    1. Hello Dean, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  7. Hey, I am using your script since a while and I like it, but today I saw, that there ist only the shortcode on that page an no reviews.
    PHP is 7.4.25 an WP still 5.8.3
    Whats happend?
    greetz Chris

    1. Hi Christian, if the shortcode is showing as text and not outputting content, it could be there is a typo in your shortcode name?

  8. Hello there,

    Is there a way to create shortcode to display only review form without the comments on the top?

    1. Hello Andre, thanks so much for your comment! Yes, this is definitely possible, but Iโ€™m afraid itโ€™s custom work. If youโ€™d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  9. I don’t quite understand with your last statement
    “Please note โ€œidโ€ is the ID of the product for which you want to output customer reviews.”

    From what I’ve understood, I need to replace the “ID”. But I’m not sure where or what’s the my source. Or where to get the ID

    1. You can get it by hovering onto any product in the WP admin

  10. how can i get to show the comments attached photos

    1. Do you use a plugin for comment attachments?

  11. Can you do this for multiple products, so that you have shortcodes for all products?

    1. Hey Austin, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  12. Is there an easy way to rewrite this to show responses to reviews (children of the original review with a depth of 2)?

    1. Hi Michael, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  13. Hi, your posts are very usefull! Is it possible to change the customer reviews text near the star rating (below the product title in the single product page). For example, “100% satisfied” instead of “9 customers review”? Thank you so mych

    1. Hello Joanna, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  14. Thanks for this – very useful! One question, how does it behave when there is a lot of comments? Will it paginate according to woocommerce settings?

    Thanks

    1. Hi David, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  15. Hi! is there any way to show the reviews with a limit up to 5 reviews per page? because I tried, it shows all the reviews and the page length is increased which I don’t want. Thanks in advance.

  16. If I can afford to post this code that allows you to view all reviews without entering the id:

    1. Sorry, edit with this ( my bad ):

      /*
       * @snippet       WooCommerce Product Reviews Shortcode
       * @how-to        Get CustomizeWoo.com FREE
       * @author        Rodolfo Melogli
       * @testedwith    WooCommerce 3.9
       * @community     https://businessbloomer.com/club/
      */
       
      add_shortcode( 'product_reviews', 'bbloomer_product_reviews_shortcode' );
       
      function bbloomer_product_reviews_shortcode() {
          
          $comments_query = new WP_Comment_Query;
          $comments = $comments_query->query( $args );
        
         $html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
          
         foreach ( $comments as $comment ) {   
            $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
            $html .= '<li class="review">';
            $html .= get_avatar( $comment, '60' );
            $html .= '<div class="comment-text">';
            if ( $rating ) $html .= wc_get_rating_html( $rating );
            $html .= '<p class="meta"><strong class="woocommerce-review__author">';
            $html .= get_comment_author( $comment );
            $html .= '</strong></p>';
            $html .= '<div class="description">';
            $html .= $comment->comment_content;
            $html .= '</div></div>';
            $html .= '</li>';
         }
          
         $html .= '</ol></div></div>';
          
         return $html;
      }
      1. This code respects the previous one Show reviews only reviews and not responses to reviews:

        /*
         * @snippet       WooCommerce Product Reviews Shortcode
         * @how-to        Get CustomizeWoo.com FREE
         * @author        Rodolfo Melogli
         * @testedwith    WooCommerce 3.9
         * @community     https://businessbloomer.com/club/
        */
         
        add_shortcode( 'product_reviews', 'bbloomer_product_reviews_shortcode' );
         
        function bbloomer_product_reviews_shortcode() {
            
        	$args = array(
        			'status' => 'approve',
        			'type' => 'review',
        			'number' => $reviews_per_page,
        			'offset' => $offset
        		);
        
        		// The Query
        		$comments_query = new WP_Comment_Query;
        		$comments = $comments_query->query( $args );
        
           $html .= '<div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
            
           foreach ( $comments as $comment ) {   
              $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
              $html .= '<li class="review">';
              $html .= get_avatar( $comment, '60' );
              $html .= '<div class="comment-text">';
              if ( $rating ) $html .= wc_get_rating_html( $rating );
              $html .= '<p class="meta"><strong class="woocommerce-review__author">';
              $html .= get_comment_author( $comment );
              $html .= '</strong></p>';
              $html .= '<div class="description">';
              $html .= $comment->comment_content;
              $html .= '</div></div>';
              $html .= '</li>';
           }
           $html .= '</ol></div></div>';
           return $html;
        }
        
        1. Cool!

          1. I want to show the product image also and not the comment author avatar.. How can I achieve this please ?

            1. Hi Amanur, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  17. I it also possible to make a shortcode for the star rating only? I want to display the stars on a different page, which php code should I use?

    1. Hey Koen, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  18. Hi, may I know how can I get and display the comment image?

    1. You mean the user avatar?

      1. The attachment but is ok. I was successful to showing this.

        1. Cool

          1. hey mate, how did you get to show the comments attached photos please

  19. hello! I have put your code on a page and the star rating does not display properly as an image.. it only displayed as “Rated 4 out of 5”..
    can you please help me how to show the star images? thanks for your help!

    1. Reu, thanks so much for your comment! Yes, this is definitely possible with a bit of CSS, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. Here is how to show stars and date:

        /*
         * @snippet       WooCommerce Product Reviews Shortcode
         * @how-to        Get CustomizeWoo.com FREE
         * @author        Rodolfo Melogli
         * @testedwith    WooCommerce 3.9
         * @community     https://businessbloomer.com/club/
        */
          
        add_shortcode( 'product_reviews', 'bbloomer_product_reviews_shortcode' );
        function bbloomer_product_reviews_shortcode() {
            $comments_query = new WP_Comment_Query;
            $comments = $comments_query->query( $args );
           
           $html .= '<div class="woocommerce"><div id="reviews" class="woocommerce-Reviews">
        	<div id="comments"><div class="woocommerce-tabs"><div id="reviews"><ol class="commentlist">';
             
           foreach ( $comments as $comment ) {   
              $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) );
              $html .= '<li class="review">';
              $html .= get_avatar( $comment, '60' );
              $html .= '<div class="comment-text">';
              if ( $rating ) $html .= wc_get_rating_html( $rating );
              $html .= '<p class="meta"><strong class="woocommerce-review__author">';
              $html .= get_comment_author( $comment );
              $html .= '</strong>&nbsp;-&nbsp;';
              $html .= smk_get_comment_time( $comment );
              $html .= '</strong></p>';
              $html .= '<div class="description">';
              $html .= $comment->comment_content;
              $html .= '</div></div>';
              $html .= '</li>';
           }
             
           $html .= '</ol></div></div></div></div></div>';
           return $html;
        }
        function smk_get_comment_time( $comment_id = 0 ){
            return sprintf( 
                _x( '%s ago', 'Human-readable time', 'text-domain' ), 
                human_time_diff( 
                    get_comment_date( 'U', $comment_id ), 
                    current_time( 'timestamp' ) 
                ) 
            );
        }
        
        1. Cool

  20. This is great, thanks for sharing. Is there a way to also add Leave your rating above or below the reviews similar to that in the woocommerce tab as well so someone can leave a review also?

    1. Hi Lori, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  21. Another super helpful snippet to learn from!

    Q: What is the best way to manually populate comments into the system? We have many to use from another application that we’d like to populate throughout the site (they are not specifically product related).

    Thanks –

    1. Hi Rick, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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