WooCommerce: get product ratings from a given product id
If you want to retrieve product ratings from a given product in WooCommerce, you can use the following simple code snippets and retrieve it via the product id.
$product = wc_get_product( $product_id ); $average = $product->get_average_rating(); $review_count = $product->get_review_count();
or like this:
$getproduct = $product_id; $average = get_post_meta( $getproduct, '_wc_average_rating', true ); $review_count = get_post_meta( $getproduct, '_wc_review_count', true );
In both examples, change $product_id to the product ID from which you want to retrieve product ratings.
It is now possible to output the values of average ratings and number of ratings respectively in PHP with $average and $review_count for that product ID.