Php Magento:单独显示评级摘要

Php Magento:单独显示评级摘要,php,magento,product,rating,review,Php,Magento,Product,Rating,Review,这是塞纳里奥: 我在后端设置了5个评级,它们是: 价值、外观、可用性、质量和便携性 但是,我需要获得每个评级的汇总投票(平均)。例如: 价值:60% 长相:50% 可用性:100% 质量:90% 可移植性:70% 评级和投票百分比是产品页面中产品所有评论的总结/平均值。Amazon.com也在使用同样的方式进行评级 我没办法弄清楚,因为Magento似乎只提供所有投票的摘要 有人能帮忙吗?我有点被这件事难住了。谢谢 你是对的,Magento使用直接非加权平均值来计算总体评价。这也很容易与客户联系

这是塞纳里奥:

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

但是,我需要获得每个评级的汇总投票(平均)。例如: 价值:60% 长相:50% 可用性:100% 质量:90% 可移植性:70%

评级和投票百分比是产品页面中产品所有评论的总结/平均值。Amazon.com也在使用同样的方式进行评级

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


有人能帮忙吗?我有点被这件事难住了。谢谢

你是对的,Magento使用直接非加权平均值来计算总体评价。这也很容易与客户联系起来(总体看起来是平均水平)。如果要使用加权平均值,则需要更改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'];
}
应输出以下内容:

rating_code => percent

Appearance => 80.6373%
Comfort => 83.2634%
Functionality => 79.7353%
希望它能帮助任何陷入同样问题的人

我也在找同样的东西。这是我的代码。
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.phtml中,然后从那里开始工作):

    
    平均值:

    看起来一般:


    请注意,
    $score->getRatingCode()
    对应于Magento Admin/Catalog/Reviews and Ratings/Manage Ratings中的评级名称。

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

     $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>
    
    
    
    通过使用具有审阅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;
    

    粘贴到review/product/view/list.phtml后,此操作不起作用(未输出任何内容)。
    $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;