Hi guys!
I'm actually a noob on magento cms.
Can someone tell me how to get the average rating of the current category?
that's what I did :
$category_id = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $category = Mage::getModel('catalog/category')->load($category_id); $products = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addCategoryFilter($category)->load(); $reviews = array(); foreach ($products as $product) { $productId = $product->getId(); array_push($reviews, Mage::getModel('review/review') ->getResourceCollection() ->addStoreFilter(Mage::app()->getStore()->getId()) ->addEntityFilter('product', $productId) ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED) ->setDateOrder() ->addRateVotes() ); } foreach ($reviews as $review) { $_votes = $review->getRatingVotes()->getFirstItem(); if($_votes-> $totalRating += $_votes->getFirstItem()->getValue(); } Zend_debug::dump(100 * $totalRating / (5 * $reviews));
Did I miss something?
thanks in advance
Solved! Go to Solution.
Okay, I solved it myself.
For those interested, here is the code:
$categoryId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $currentCategory = Mage::getModel('catalog/category')->load($categoryId); $productList = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addCategoryFilter($currentCategory)->load(); $reviewList = array(); $storeId = Mage::app()->getStore()->getId(); foreach ($productList as $product) { $summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)->load($product->getEntityID()); $reviewData[] = $summaryData['rating_summary'] / 20; } $nbReview = 0; foreach ($reviewData as $review) { if ($review != Null) $nbReviews++; $totalReviewIntoStar += $review; } return $AvgRating = $totalReviewIntoStar / $nbReviews;
Okay, I solved it myself.
For those interested, here is the code:
$categoryId = Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); $currentCategory = Mage::getModel('catalog/category')->load($categoryId); $productList = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('*')->addCategoryFilter($currentCategory)->load(); $reviewList = array(); $storeId = Mage::app()->getStore()->getId(); foreach ($productList as $product) { $summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)->load($product->getEntityID()); $reviewData[] = $summaryData['rating_summary'] / 20; } $nbReview = 0; foreach ($reviewData as $review) { if ($review != Null) $nbReviews++; $totalReviewIntoStar += $review; } return $AvgRating = $totalReviewIntoStar / $nbReviews;
Hi @anthony_ip,
Are you using that calc everytime the category is rendered?
In taht case, did you see some performance issue?
I indeed use this calc in each render.
I didn't check the perf