这是情景:
我在后端设置了 5 个评级,它们是:价值、外观、可用性、质量和便携性
但是,我需要获得每个评分的汇总投票(平均)。例如: 价值:60% 外观:50% 可用性:100% 质量:90% 便携性:70%
评分和投票百分比是产品页面中产品所有评论的汇总/平均值。Amazon.com 也使用相同的样式进行评分。
我无法弄清楚,因为 Magento 似乎只提供开箱即用的所有选票摘要。
有人可以帮忙吗?我有点坚持这一点。谢谢!
所以为了回答我自己的问题,这就是我所做的。
app\code\local\Mage\Catalog\Block\Product\View\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%
希望它可以帮助任何陷入同样问题的人!
这是价值和外观的即插即用解决方案(只需将代码粘贴到 review/product/view/list.phtml 并从那里开始工作):
<?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 管理/目录/评论和评级/管理评级中的评级名称。
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
您可以使用以下代码获取 Rating 集合:
$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>
你是对的,Magento 使用直接的非加权平均值来计算整体评论。这也很容易与客户联系起来(整体看起来像一个平均水平)。如果您想使用加权平均值,您需要更改 Magento 用于计算平均评分量的代码。
一旦您为此付出了一些努力,请随时回来提出您对该过程的任何问题。
希望有帮助!
谢谢,乔
通过使用带有评论 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;