Menu Close

How to Display WooCommerce Product Reviews on Custom Page with a Shortcode

Codeamend

Let’s see how to get the WooCommerce product customer reviews using a shortcode.

Refer the below snippet to get specific product customer reviews…

add_shortcode( 'product_reviews', 'cwa_product_reviews_shortcode' );
 
function cwa_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"><ul 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 .= '</ul></div></div>';

    return $html;
} 

Update this php code in function.php and use shorcode [product_reviews id=”product ID”] to get custom product customer reviews.

Posted in WooCommerce, WordPress, WordPress Shortcodes

You can also read...