Display product’s rating beyond the review page

Reviews-featured

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:

product-rating-example

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.

3
Top

Care to rate this post?

Author

Petar Sambolek

Ex Inchooer

Petar worked at Inchoo as a Backend Developer from January to August 2014

Other posts from this author

Discussion 3 Comments

Add Comment
  1. vivek

    Hi,
    Thanks for the code.

    I also want to show the rating title and the comment add by customer.
    Please help

  2. Sheldon

    What File(s) Are you Editing ?

    Thanks for the Article

  3. Hi, Sheldon, vivek.
    In this example I edited the checkout template file.
    app/design/frontend/base/default/template/checkout/cart/item/default.phtml

    @vivek By the sound of it, what you need are reviews, i.e. Magento_Review_Model_Review model, which is not a subject of this article. Hope this helps you in your search :)

Add Your Comment

Please wrap all source codes with [code][/code] tags.
Top