Subesh Pokhrel

Magento Developers Blog

Getting Rescent Rating Summary of a Product in Magento

Magento has a default option to show the overall rating’s summary of the product, which is shown in the product view page. This summary is based on all the review posted and approved for the particular product. In the summary the stars are shown accordingly, by taking the sum of all reviews and calculating the average rating. But I had a case where I needed to show those star ratings, only based on the last approved user ratings. What I did was just a small tweak in the Magento’s default working! Magento gets all the reviews and processes it accordingly, so what I did was just added limit filter to get the rescent rating only, and left the Magento default working do the rest. Tricky! huh…. Here the block that can achieve the same. [source language=”php”] getReviewsCollection()->setPageSize(1); return $collection; } /** * Getting Current Product’s name */ public function getProductName () { return Mage::registry(“product”)->getName(); } /** * Getting Rating summary only for the one Review (latest one) */ public function getRatingSummary () { return Mage::getModel(‘rating/rating’)->getReviewSummary($this->getReviewId()); } } [/source] And here is the phtml file. [source language=”html”] getRescentReview()->getItems(); ?> load(array_keys($_items)); $curRat=$_review->getData(‘review_id’); // Setting the current Review Id to the block’s Object, used in Namespace_Comment_Block_Comment line #26 $this->setReviewId($curRat); if($curRat!=1): ?>

__(‘Recente Reviews’) ?>

getProductName()?>

– [/source] By the end of this you will get the Rescent rating summary on your product detail page. Happy Coding in Magento!!