Site icon Qaisar Satti's Blogs

How to Add Star Rating in WooCommerce

Today we will discuss how you can add star ratings to your WooCommerce store pages. This can be done by pasting the following code in your active/child theme’s function.php file.

add_action('woocommerce_after_shop_loop_item', 'get_star_rating' );
function get_star_rating()
{
    global $woocommerce, $product;
    $average = $product->get_average_rating();

    echo '<div class="star-rating"><span style="width:'.( ( $average / 5 ) * 100 ) . '%"><strong itemprop="ratingValue" class="rating">'.$average.'</strong> '.__( 'out of 5', 'woocommerce' ).'</span></div>';
}

Another solution.

$product = wc_get_product( $id );
echo wc_get_rating_html( $product->get_average_rating() );
Exit mobile version