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.

20
Top

Care to rate this post?

Author

Petar Sambolek

Backend Developer

Petar is a Backend Developer who is, alongside programming, also interested in business side of online stores. He’s bribable with sweets.

Other posts from this author

Discussion 20 Comments

Add Comment
  1. sergiu

    where do i have to insert this code ?

  2. Sam

    How do you echo out the rating summary as the ’5 stars’ using this method?

  3. Sam

    As always, answered by own question just after I posted!

    $storeId = Mage::app()->getStore()->getId();
    $summaryData = Mage::getModel(‘review/review_summary’)
    ->setStoreId($storeId)
    ->load($_product->getId());
    echo ‘
    getRatingSummary().’%”>
    ‘;

  4. As always, you at Inchoo provide kick-ass Magentutorials ;-)

  5. tom

    i was to open a review page on my e commerce site and put a new product on the spotlight for customers to review / or just add the current data from the product page

    where do i insert this code? i tried via the CMS but it’s not working

    any suggestions ?

  6. How to get the review success message from the session on review page?

    Please provide the solution..Thanks in Advance.

  7. How to display Recent Review on my home page?

  8. max

    how do i add a product review pagination?

  9. getStore()->getId();
    $summaryData = Mage::getModel(‘review/review_summary’)
    ->setStoreId($storeId)
    ->load($_product->getId())->getRatingSummary();
    ?>

    Get stare rating summary

  10. But how to get star rating for each individual review ??

  11. As always answered my own question :-)

     $reviewcollection =  Mage::getModel('review/review')->getCollection()
    						->addStoreFilter(Mage::app()->getStore()->getId())
    						->addStatusFilter('approved')
    						->addRateVotes()
    						->addEntityFilter('product', $this->getProduct()->getId())
    						->setDateOrder();
    

    * added ->addRateVotes()

  12. mark

    hi,
    Any one please tell me the pagination for the reviews in front end…

  13. Tung

    Hi, now i doing select all products that have review highest, or vote % highest, but when select with ->join, magento generate sql statement ok, but collection not ok. More detail, i need order by rating_summary but collection products order by entity_id. Actual i don’t understand. This is my code:

    			$collection = Mage::getModel('catalog/product')
    					->getResourceCollection()
    					->addAttributeToSelect('*')
    					->setOrder('rating_summary', 'desc')
    					->setPageSize($this->getLimit())
    					->load()
    					;
    		$collection->getSelect()
    				->join(array('res'=>'review_entity_summary'), 'res.entity_pk_value = e.entity_id', array('res.rating_summary'));
    		$collection->getSelect()->where('store_id ='.$this->getStore())
    				;
    		$collection->getSelect()->order('res.rating_summary desc');		 

    Hope for soon reply. Thank

  14. Harita

    i want to display reviews of new product on home page? please tell me where to put this code?…

  15. Yudha

    @Harita, if you get an answer, let me know. Thanks

  16. valy

    I’m getting a blank page when using the code from this post. Is there anything i’m missing?

  17. This is really good. I was using this information about getting the reviews several times so far. Thank you

  18. Guillermo

    Hi there, where this code should be added?, i get a blank page when i used this code.

    Thanks

  19. how to display top rated products in home page

  20. Alex

    Hi! I want to display review form for each product on category page..how can I do it? Could anyone help? By this moment I can see review form template for each product, but then I press submit review nothing happens and in backend review didn’t appear. Thanks in advance.

Add Your Comment

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