这是senario:
我在后端设置了5个评分,它们是: 价值,外观,可用性,质量和便携性
但是,我需要获得每个评级的摘要投票(平均值)。一个例子是: 价值:60% 搭配:50% 可用性:100% 质量:90% 便携性:70%
评分和投票%是产品页面中产品评论的摘要/平均值。亚马逊也在评级中使用相同的风格。
我无法理解,因为Magento似乎只提供所有选票的摘要。
有人可以帮忙吗?我有点坚持这个。谢谢!
答案 0 :(得分:1)
所以回答我自己的问题,这就是我所做的。
应用\代码\本地\法师\目录\块\产品\视图\ 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%
希望它可以帮助任何人遇到同样的问题!
答案 1 :(得分:0)
你是对的,Magento使用直接的非加权平均值来计算整体评论。这也很容易与客户联系起来(整体看起来像平均值)。如果您想使用加权平均值,则需要更改Magento用于计算平均评级金额的代码。
一旦你付出了一些努力,请随时回答你对该过程的任何疑问。
希望有所帮助!
谢谢, 乔
答案 2 :(得分:0)
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
答案 3 :(得分:0)
以下是Value and Looks的即插即用解决方案(只需将代码粘贴到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管理员/目录/评论和评级/管理评级中的评级名称。
答案 4 :(得分:0)
您可以使用以下代码获得评分收集:
$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>
答案 5 :(得分:0)
通过使用具有评论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;