- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-05-2018
06:14 AM
04-05-2018
06:14 AM
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.
Labels:
1 ACCEPTED SOLUTION
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2018
08:10 AM
04-06-2018
08:10 AM
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;
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-06-2018
08:10 AM
04-06-2018
08:10 AM
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;
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-09-2018
08:59 PM
04-09-2018
08:59 PM
Re: Get the average rating of the current category
Hi @anthony_ip,
Are you using that calc everytime the category is rendered?
In taht case, did you see some performance issue?
- Mark as New
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-10-2018
12:47 AM
04-10-2018
12:47 AM
Re: Get the average rating of the current category
I indeed use this calc in each render.
I didn't check the perf