skip to Main Content

If my web pages show many products (include title and rating), then web page needs to calculate rating percentage by every products with following code.
I am sure this kinds of calculation will slow down my web page.
Is this the only choice that i can choose, or is there any better performance code that i can use?

            if (count($reviews) > 0) 
            {
                foreach ($reviews->getItems() as $review) 
                {
                    foreach( $review->getRatingVotes() as $vote ) 
                    {
                        $ratings[] = $vote->getPercent();
                    }
                }
                $avg = array_sum($ratings)/count($ratings);
            }

3

Answers


  1. you can used this code, magento use same code on category list page.

    <?php if($_product->getRatingSummary()): ?>
          <?php echo $this->getReviewsSummaryHtml($_product) ?>
    <?php endif; ?>
    

    You need to just pass your $product object in this code.

    Login or Signup to reply.
  2. You can use review model of Magento’s core

    <?php
    $store_id = Mage::app()->getStore()->getId(); //get store id
    
    //in your product loop 
    
    $review_summary = Mage::getModel('review/review_summary')->setStoreId($store_id)->load($_product->getId()); //pass product's id to review model
    echo $review_summary['rating_summary']; //get rating summary
    
    ?>
    
    Login or Signup to reply.
  3. You can use below code to get rating on any page:

    <?php
        $storeId = Mage::app()->getStore()->getId();
        $summaryData = Mage::getModel('review/review_summary')->setStoreId($storeId)  ->load($_product->getId());
        ?>
    <?php if($_product->getRatingSummary()): ?> 
    <?php if($summaryData['rating_summary']){ ?><div class="rating-box"><div class="rating" style="width:<?php echo $summaryData['rating_summary']; ?>%"></div></div>(<?php echo $_product->getRatingSummary()->getReviewsCount(); ?>)<?php } ?>
    <?php endif; ?>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search