Luis Valencia
Luis Valencia

Reputation: 34038

Magento, Get Ratings as a number in php

I am trying to change my template to reflect the schema.org html The only problem I have had so far is the reviews average which should be a number from 0 to 5.

there is a function called getratingssummary, but its used inside the style attribute of a div, I think it does not return a number

so far I have this

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

Upvotes: 1

Views: 3966

Answers (3)

Dan Norris
Dan Norris

Reputation: 413

To get the numeric star rating as opposed to the percentage open your desired review helper which could be either;

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

or

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


And use this code to get your number;

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

Upvotes: 3

Andrew Chok
Andrew Chok

Reputation: 11

You can try this, but you have to make it work to show a number instead of percentage:

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

Upvotes: 1

CCBlackburn
CCBlackburn

Reputation: 1704

The getRatingSummary function does return a numeric value, it just happens to be a percentage. If you have a look at the way it's called in the style attribute of the rating div you will see (if you replace the function call with an X)...it's definitely bringing back a percentage.

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

I would echo the $this->getRatingSummary() function call and see what you are getting back, then you can work on the maths to turn it into a 0-5 value.

Upvotes: 1

Related Questions