Home » WooCommerce: Display Product Reviews @ Custom Page (Shortcode)

WooCommerce: Display Product Reviews @ Custom Page (Shortcode)

by adminkys

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 Bloomer Armada 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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @testedwith    WooCommerce 3.9
 * @donate $9     https://www.tutoraspire.com
 */

add_shortcode( 'product_reviews', 'tutoraspire_product_reviews_shortcode' );

function tutoraspire_product_reviews_shortcode( $atts ) {

if ( empty( $atts ) ) return '';

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

$comments = get_comments( 'post_id=' . $atts['id'] );

if ( ! $comments ) return '';

$html .= '
    '; foreach ( $comments as $comment ) { $rating = intval( get_comment_meta( $comment->comment_ID, 'rating', true ) ); $html .= '
  1. '; $html .= get_avatar( $comment, '60' ); $html .= '
    '; if ( $rating ) $html .= wc_get_rating_html( $rating ); $html .= '

    '; $html .= get_comment_author( $comment ); $html .= '

    '; $html .= '
    '; $html .= $comment->comment_content; $html .= '
    '; $html .= '
  2. '; } $html .= '
'; return $html; }

You may also like