Menu Close

How to display WooCommerce product reviews using a shortcode?

Codeamend

Let’s see how to display WooCommerce product reviews using a shortcode.

Refer the below snippet to get custom product review details…

add_shortcode( 'productrating', 'cwa_productrating_func' );

function cwa_productrating_func( $atts ) {
    $product = wc_get_product( $atts['id'] );
    $rating_count = $product->get_rating_count();
    $review_count = $product->get_review_count();
    $average      = $product->get_average_rating();
    
    $rating = '<div class="woocommerce">';
    if ( $rating_count > 0 ) :
	$rating .= '<div class="woocommerce-product-rating">';
	$rating .= wc_get_rating_html( $average, $rating_count ).'('.$review_count.' customer reviews)';
    $rating .= '</div>';
    endif;
    $rating .= '</div>';
    return $rating;
} 

Update this php code in function.php and use shorcode [productrating id=”product ID”] to get custom product review count and star ratings.

Posted in WooCommerce, WordPress, WordPress Shortcodes

You can also read...