Reviews are another one of numerous excellent features in Magento. Usage of reviews can increase visitors’ trust in your brand, which, in return boosts your sales by a significant amount.
Sometimes there is a need for showing star ratings outside the products’ review page. That’s what I’ll be addressing with this article.
The following code snippet works just about anywhere you have a product loaded (or if you know it’s ID) – cart being one of the examples.
<?php
$_product = $_item->getProduct(); //get the product in cart
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')
->setStoreId($storeId)
->load($_product->getId());
if ($summaryData['rating_summary']):?>
<div class="ratings">
<div class="rating-box">
<div class="rating" style="width:<?php echo $summaryData['rating_summary']; ?>%"></div>
</div>
</div>
<?php endif; ?>
If we visit our cart page, we can see that rating stars are shown for every product customers have rated:
The array of keys for values we can access in $summaryData object are as follows:
//Entity id of a summary review
["primary_id"] => string(3) "100"
//
//Product id
["entity_pk_value"] => string(3) "119"
//
//Entity type id: 1-Product; 2-Customer; 3-Category
["entity_type"] => string(1) "1"
//
//Qty of reviews
["reviews_count"] => string(1) "2"
//
//Summarized rating: the percentage which represents number of stars, each 20% step fills up one star
["rating_summary"] => string(2) "80"
//
//Store id
["store_id"] => string(1) "1"
Keep in mind that the “Magento way” of showing this would be creating a block or a helper with a method that accepts two parameters – product ID and store ID, and echoing it’s return value from the view file.
However, you could also insert this code snippet anywhere in the view file for quick fetch of star ratings for your product.
Be careful to echo the rating value following the HTML markup (lines 7-11) above, otherwise, the stars won’t be shown.
As always, you can view my code example on GitHub
Note: This is a revamp of the article originally written in January 2009.