Magento:单独显示评级摘要


Magento: Show Rating Summary Individually

情况如下:

我在后端设置了5个评级,它们是:价值、外观、可用性、质量和可移植性

但是,我需要得到每个评级的汇总投票(平均值)。一个例子是:值:60%外观:50%可用性:100%质量:90%可移植性:70%

评分和投票百分比是产品页面中对该产品的所有评论的汇总/平均值。亚马逊网站也在使用同样的风格进行评分。

我弄不清楚,因为Magento似乎只提供所有投票的摘要。

有人能帮忙吗?我有点被困住了。谢谢!

所以回答我自己的问题,这就是我所做的。

应用程序代码' '本地块'法师'目录' '产品'视图' RatingSummary.php

class Mage_Catalog_Block_Product_View_RatingSummary extends Mage_Core_Block_Template
{
    public function getRatingAverages() {
        $rating = array();
        $product_id = Mage::registry('product')->getId();
        $resource = Mage::getSingleton('core/resource');
        $cn= $resource->getConnection('core_read');
        $sql = "select rt.rating_code, 
                avg(vote.percent) as percent from ".$resource->getTableName('rating_option_vote')." as vote 
                inner join ".$resource->getTableName('rating')." as rt
                on(vote.rating_id=rt.rating_id)
                inner join ".$resource->getTableName('review')." as rr
                on(vote.entity_pk_value=rr.entity_pk_value)
                where rt.entity_id=1 and vote.entity_pk_value=$product_id and rr.status_id=1
                group by rt.rating_code";
        $rating = $cn->fetchAll($sql);
        return $rating;
    }
}

我无法找到使用现有Magento api单独获得评级的方法。也许还有别的办法,但我不知道怎么做。所以我必须手动运行纯SQL查询数据库,这不是最好的方式。基本上,上面的代码返回以下内容:

rating_code => percent
Appearance => 80.6373%
Comfort => 83.2634%
Functionality => 79.7353%

在我的模板中,我使用了类似的东西来显示结果

$rating_averages = $this=>getRatingAverages();
foreach($rating_averages as $rating) {
   echo $rating['rating_code'] . " => " . $rating['percent'];
}

应该输出以下内容:

Appearance => 80.6373%
Comfort => 83.2634%
Functionality => 79.7353%

希望它能帮助任何陷入同样问题的人!

你是正确的,Magento使用直接的非加权平均值来计算总体评论。这也很容易与客户联系起来(整体看起来像一个平均值)。如果您想使用加权平均值,则需要更改Magento用于计算平均评级金额的代码。

一旦你在这方面付出了努力,如果你对这个过程有任何问题,请随时回来。

希望有帮助!

谢谢,乔

I was looking for same thing.Here is my code.
<?php
$_reviewsCollection = Mage::getModel('review/review')->getCollection()
                ->addStoreFilter(Mage::app()->getStore()->getId())
                ->addStatusFilter('approved')
                ->addEntityFilter('product', $this->getProduct()->getId())
                ->setDateOrder()
                ->addRateVotes()
                ->load();
$_votesArray=array();
$_totalReview=0;
foreach ($_reviewsCollection->getItems() as $_review){   $_totalReview++;
$_votes = $_review->getRatingVotes(); 
    if (count($_votes)){
        foreach ($_votes as $_vote){
                $_votesArray[$_vote->getRatingCode()]=$_votesArray[$_vote->getRatingCode()]+$_vote->getPercent();
        }
    }
}
//print_r($_votesArray);    
?>
<ul>
<?php foreach ($_votesArray as $_key=>$_data){ ?>
                        <li><span><?php echo $_key ?>:</span>
                        <div class="rating-box">
                            <div class="rating" style="width:<?php echo $_data/$_totalReview ?>%"></div>
                        </div>
                        </li>
<?php } ?>  
</ul

这是一个值和外观的即插即用解决方案(只需将代码粘贴到review/product/view/list中)。php和从那里开始工作):

<?php
$_items = $this->getReviewsCollection()->getItems();
$valueRatings = array();
$looksRatings = array();
$valueAvg = '';
$looksAvg = '';
if (count($_items) > 0) {
  foreach ($_items as $review) {
    foreach($review->getRatingVotes() as $score) {
      switch ($score->getRatingCode()) {
        case 'Value':
          $valueRatings[] = $score->getPercent();
          break;
        case 'Looks':
          $looksRatings[] = $score->getPercent();
          break;
      }
    }
  }
  $valueAvg = array_sum($valueRatings) / count($valueRatings);
  $looksAvg = array_sum($looksRatings) / count($looksRatings);
}
?>
<p>VALUE Average: <?php echo $valueAvg ?></p>
<p>LOOKS Average: <?php echo $looksAvg ?></p>

注意,$score->getRatingCode()对应于Magento Admin/Catalog/Reviews and Ratings/Manage Ratings中的Rating Name。

您可以使用以下代码获取评级集合:

 $collection = Mage::getModel('rating/rating')
        ->getResourceCollection()
        ->addEntityFilter('product')
        ->setPositionOrder()
        ->setStoreFilter(Mage::app()->getStore()->getId())
        ->addRatingPerStoreName(Mage::app()->getStore()->getId())
        ->load();
$collection->addEntitySummaryToItem($_product->getId(), Mage::app()->getStore()->getId());

你可以在/app/code/core/Mage/Rating/Block/Entity/Detailed.php中找到这些代码

然后显示每个评级:

<?php if(!empty($collection) && $collection->getSize()): ?>
<table class="ratings-table">
    <col width="1" />
    <col />
    <tbody>
        <?php foreach ($collection as $_rating): ?>
            <?php if($_rating->getSummary()): ?>
                <tr>
                    <th><?php echo $this->__($this->escapeHtml($_rating->getRatingCode())) ?></th>
                    <td>
                        <div class="rating-box">
                            <div class="rating" style="width:<?php echo ceil($_rating->getSummary()) ?>%;"></div>
                        </div>
                    </td>
                </tr>
            <?php endif; ?>
        <?php endforeach; ?>
    </tbody>
</table>

通过使用database with review id

$objectManager = 'Magento'Framework'App'ObjectManager::getInstance();
$resource = $objectManager->get('Magento'Framework'App'ResourceConnection');
$connection = $resource->getConnection();
$rating_option_vote = $resource->getTableName('rating_option_vote'); //gives table name with prefix
$review_id = 1;
//Select Data from table
$sql_review = "Select * FROM " . $rating_option_vote ." where `review_id` =".$review_id;
$result_review = $connection->fetchAll($sql_review); // gives associated array, table fields as key in array.
$percentage = 0;
if(!empty($result_review)){
    $percentage = $result_review[0]['percent'];
}
echo $percentage;