2

I use 5 rating options for a product review.
It is displayed with the well known stars, see below.

enter image description here

But I want to also display the total score.
A score that display the average score of the total 5 of that score.

(I do not want to display the average of all reviews, only the average of each individual review)

This is my detailed.phtml

    <?php $_votes = $this->getRatingVotes($_review->getId());?>
    <?php if (count($_votes)): ?>
    <table class="ratings-table">
        <col width="1" />
        <col />
        <tbody>
            <?php foreach ($_votes as $_vote): ?>
            <tr>
                <th><?php echo $_vote->getRatingCode() ?></th>
                <td>
                    <div class="rating-box">
                        <div class="rating" style="width:<?php echo $_vote->getPercent() ?>%;"></div>
                    </div>
                </td>
            </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
    <?php endif; ?>

How can I display this?

2

As you mentioned that you want only the average of each individual review along the score of each vote, you should calculate the average in the foreach loop. change your code like this:

<?php $_votes = $this->getRatingVotes($_review->getId());?>
<?php $_votes_average = 0; ?>
<?php if (count($_votes)): ?>
<table class="ratings-table">
    <col width="1" />
    <col />
    <tbody>
      <?php foreach ($_votes as $_vote): ?>
        <?php $_votes_average += $_vote->getPercent(); ?>
        <tr>
            <th><?php echo $_vote->getRatingCode() ?></th>
            <td>
                <div class="rating-box">
                    <div class="rating" style="width:<?php echo $_vote->getPercent() ?>%;"></div>
                </div>
            </td>
        </tr>
      <?php endforeach; ?>
    </tbody>
</table>
<?php $_votes_average = $_votes_average / count($_votes); ?>
<?php endif; ?>

<?php /* This is average rating: */ ?>
<div class="rating-box">
    <div class="rating" style="width:<?php echo $_votes_average; ?>%;"></div>
</div>
1

The rating stars are located in app/design/frontend/[packagage]/[template]/rating/detailed.phtml

You could tweak it like below where you add all the ratings up and devide them by the number of ratings to get the average.

$totalScore = 0;
if(!empty($collection) && $collection->getSize()): ?>
    <table class="ratings-table">
        <col width="1" />
        <col />
        <tbody>
            <?php foreach ($collection as $_rating): ?>
                <?php if($_rating->getSummary()): $totalScore += $_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; ?>
                <tr>
                    <th>Total score</th>
                    <td>
                        <div class="rating-box">
                            <div class="rating" style="width:<?php echo ceil($totalScore / $collection->getSize()) ?>%;"></div>
                        </div>
                    </td>
                </tr>
        </tbody>
    </table>
<?php endif; ?>
  • I do got a other detailed.phtml. See my edit. How can I display the total score with my code? – Henk Z May 26 '15 at 15:00
  • You're code doesn't differ that much from mine. The basic principle stands. Add all the percentages, devide by the number of types of ratings which gives you the avg. percentage – Sander Mangel May 26 '15 at 15:11
  • I know, but I can't get it fixed. How do I need to edit your code to make it work? – Henk Z May 26 '15 at 15:17
  • Sander can you help me out? I just can't find the right code for this. – Henk Z May 29 '15 at 7:17
1

Please goto list.phtml file and edit path of magento root directory: app\design\frontend\MyPackage\MyTheme\template\review\product\view\list.phtml

Add the below code as you require to display on product page's review section of front-end after admin configure review rating summary.

<?php  
    $_product = Mage::registry('product'); 
    $_productId = $_product->getId();
    $storeId = Mage::app()->getStore()->getId();
    $summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)->load($_product->getId());  
    $reviewsCount = Mage::app()->getLayout()->createBlock('review/product_view')->getReviewsCollection()->getSize(); 

    ?> 
    <div class="review-star-rating">
            <div class="star-rating"><?php echo (($summaryData['rating_summary'] * 5) / 100) ; ?></div>  
            <div class="rating_text"><?php echo $this->__('Average Rating Based on '.$reviewsCount.' ratings') ?></div> 
    </div>

Hope this helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.