Magento,在php中以数字形式获取评级


Magento, Get Ratings as a number in php

我正在尝试更改模板以反映schema.org.html到目前为止,我遇到的唯一问题是评论的平均值,应该是0到5之间的数字。

有一个名为getratingssummary的函数,但它在div的style属性中使用,我认为它不会返回数字

到目前为止,我有这个

<?php if ($this->getReviewsCount()): ?>
    <div class="ratings">
        <?php if ($this->getRatingSummary()):?>
          <span itemprop="aggregateRating" itemscope itemtype="http://schema.org/AggregateRating">
            <div class="rating-box">
                <div class="rating" style="width:<?php echo $this->getRatingSummary() ?>%"><meta itemprop="ratingValue" content="5"/></div>
                <span itemprop="reviewCount"><?php echo $this->getReviewsCount() ?></span>
            </div>
          </span>
        <?php endif;?>
        <p class="rating-links">
            <a href="<?php echo $this->getReviewsUrl() ?>"><?php echo $this->__('%d Review(s)', $this->getReviewsCount()) ?></a>
            <!--<span class="separator">|</span>-->
            <!--<a href="<?php echo $this->getReviewsUrl() ?>#review-form"><?php echo $this->__('Add Your Review') ?></a>-->
        </p>
    </div>

要获得数字星级,而不是百分比,请打开您想要的评论助手,可以是;

app/design/frontend/default/YOURTHEME/template/review/helper/summary_short.phtml

app/design/frontend/default/YOURTHEME/template/review/helper/summary.phtml


并使用此代码获取您的号码;

<?php
$ratingPercent = $this->getRatingSummary();
echo ($ratingPercent * 5)/100;
?>

getRatingSummary函数确实返回了一个数值,它恰好是一个百分比。如果你看看它在ratingdiv的style属性中的调用方式,你会看到(如果你用X替换函数调用)。。。这肯定会带来一定的回报。

<div class="rating" style="width:X%">

我会回显$this->getRatingSummary()函数调用,看看你得到了什么,然后你可以计算出0-5的值。

你可以尝试一下,但你必须让它显示一个数字而不是百分比:

<?php
$storeId = Mage::app()->getStore()->getId();
$summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)  ->load($_product->getId());
?>                      
// Rating Percentage showing of a product
 <div class="rating"><?php echo $summaryData['rating_summary']; ?>%</div>