Wordpress - Get rating product by product id

Given a product ID you can get the average rating like this:

$product = wc_get_product( $product_id );
$rating  = $product->get_average_rating();

That'll return the raw number (4.00, 3.50 etc.).

To output the rating HTML for a given product you can use this code:

$product = wc_get_product( $product_id );
$rating  = $product->get_average_rating();
$count   = $product->get_rating_count();

echo wc_get_rating_html( $rating, $count );

Or, if you're in the loop you can use this function to get the HTML for the current product:

woocommerce_template_loop_rating() 

Tags:

Rating