1

I'm new to magento and i'm currently working on the CE1.9 for work. I'm having trouble to get the average rating of the current category.

$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));

1 Answer 1

1

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;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.