获得当前类别的平均评级Magento 1.9 CE

时间:2018-04-06 10:40:57

标签: php magento magento-1.9

我是magento的新手,我目前正在从事CE1.9工作。我无法获得当前类别的平均评分。有人能帮助我吗?

$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();
    $totalRating += $_votes->getFirstItem()->getValue();
}
Zend_debug::dump(100 * $totalRating / (5 * $reviews));

我错过了什么吗?

提前致谢

1 个答案:

答案 0 :(得分:0)

好的,我自己解决了。

对于那些感兴趣的人,这里是代码:

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