RSS

how to get average product rating on a page in magento

29 Dec

1) if you intend to display the average rating of each product, add this helper method inside the page(like list.phtml):

<?php
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)  ->load($_product->getId());
?>                                                                          
// Rating Percentage showing of a product
<div class="rating">(<?php echo $summaryData['rating_summary']; ?>%)</div>  

2) Get the product Review anywhere in magento

<?php // review of a product at any page
$_reviews = Mage::getModel('review/review')->getResourceCollection();
$_reviews->addStoreFilter( Mage::app()->getStore()->getId() )
->addEntityFilter('product', $product->getId())
->addStatusFilter( Mage_Review_Model_Review::STATUS_APPROVED )
->setDateOrder()
->addRateVotes();
$avg = 0;
$ratings = array();
if (count($_reviews) > 0){
foreach ($_reviews->getItems() as $_review): ?>
<?php foreach( $_review->getRatingVotes() as $_vote ): ?>
<?php $ratings[] = $_vote->getPercent(); ?>
<?php endforeach; ?>
<?php endforeach;
$avg = array_sum($ratings)/count($ratings); }
?>
<?php if($avg > 0):?>

<div class=”ratings”>
<div class=”rating-box”>
<div class=”rating” style=”width: <?php echo ceil($avg) ; ?>%;”></div>
</div>
</div>

<?php endif;?>

3) Get the Review Count of a product in any page like list.phtml

<?php                                                                         // review count of a product
echo $reviewCount = $_product->getRatingSummary()->getReviewsCount() ? $_product->getRatingSummary()->getReviewsCount(): 0;
?>
 
1 Comment

Posted by on Dec 29, 2011 in Magento

 

Tags: , ,

One response to “how to get average product rating on a page in magento

  1. David

    Aug 14, 2013 at 2:42 pm

    This is very useful post. It works like a charm…….thanks very much.

     

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: