【问题标题】:Magento, Get Ratings as a number in phpMagento,在 php 中以数字形式获取评分
【发布时间】:2012-09-08 11:44:27
【问题描述】:

我正在尝试更改我的模板以反映 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>

【问题讨论】:

    标签: php zend-framework magento magento-1.4 magento-1.6


    【解决方案1】:

    要获得数字星级评分,而不是打开所需的评论助手的百分比,这可能是;

    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;
    ?>
    

    【讨论】:

      【解决方案2】:

      getRatingSummary 函数确实返回一个数值,它恰好是一个百分比。如果你看一下在评分 div 的样式属性中调用它的方式,你会看到(如果你用 X 替换函数调用)......它肯定会带回一个百分比。

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

      我会回显 $this->getRatingSummary() 函数调用,看看你得到了什么,然后你可以通过数学把它变成一个 0-5 的值。 p>

      【讨论】:

        【解决方案3】:

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

        <?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>  
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2019-11-22
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2021-11-19
          相关资源
          最近更新 更多