Magento: How to get product rating and review?

Here is the code to get ratings and reviews for any particular product.

/**
 * Getting reviews collection object
 */
$productId = $product->getId();
$reviews = Mage::getModel('review/review')
				->getResourceCollection()
				->addStoreFilter(Mage::app()->getStore()->getId())
				->addEntityFilter('product', $productId)
				->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
				->setDateOrder()
				->addRateVotes();
/**
 * Getting average of ratings/reviews
 */
$avg = 0;
$ratings = array();
if (count($reviews) > 0) {
	foreach ($reviews->getItems() as $review) {
		foreach( $review->getRatingVotes() as $vote ) {
			$ratings[] = $vote->getPercent();
		}
	}
	$avg = array_sum($ratings)/count($ratings);
}

Looking for improvement for your Magento Store?
Facebook Connector Extension
Product related article extension
Magento Offer popup on exit extension
Unlimited Slideshow Magento Extension
Light Checkout in One Step Magento Extension

Displaying the ratings

<!-- REMOVE 'IF CONDITION', TO SHOW RATING IN ALL RATED/UNRATED PRODUCTS -->
<?php if($avg): ?>
	<div class="rating-box" style="float:left;">
		<div class="rating" style="width: <?php echo ceil($avg) ; ?>%;"></div>
	</div>
<?php endif; ?>

Hope this helps. Thanks.


Leave a Reply

Your email address will not be published.

16 + twelve =

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>