0

I want to load all reviews and all ratings of a specific product using product id so i have made a module but problem is that i am not able to load detail reviews and detail rating of any specific product using product id only. i want to use it on categories page. i want this

enter image description here and i have completed jquery part only product loading part is left please help me as soon as possible. I want to use foreach command not any other command.

2 Answers 2

4

you use below script to get all reveiew and rating .

 <?php
require_once('app/Mage.php');
Mage::app();

$_product=Mage::getModel('catalog/product')->load(1);

$productId = $_product->getId();
$reviews = Mage::getModel('review/review')
                ->getResourceCollection()
                ->addStoreFilter(Mage::app()->getStore()->getId())
                ->addEntityFilter('product', $productId)
                ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED)
                ->setDateOrder()
                ->addRateVotes();

$avg = 0;
$ratings = array();
if (count($reviews) > 0) {
    foreach ($reviews->getItems() as $review) {
        foreach( $review->getRatingVotes() as $vote ) {
            Mage::log('2 if');
            Mage::log($vote->getData());
            $ratings[] = $vote->getPercent();
        }
    }

    $avg = array_sum($ratings)/count($ratings);
}
echo 'avg'.$avg;

?>
5
  • If above answer will help you, please accept it, So it will help other in community. Commented Dec 30, 2016 at 12:10
  • There is a big problem with this coding. Basically % of rating generated by your coding is different from rating seen on product page. I am using magento1924. I magento sample data product "Racer Back Maxi Dress" product detail page rating % is 100, 93.33, 53.33, 80, 93.33. But accourding to your coding it is 100, 100, 40, 80, 100. I do not know that where is problem. Commented Dec 30, 2016 at 15:55
  • you print log of object and check. i used this coode in my axtension and it works fine Commented Dec 31, 2016 at 5:25
  • I get all solution Commented Feb 7, 2017 at 19:05
  • And solution is below Commented Feb 9, 2017 at 15:21
3
 <?php 
 $productId = Mage::app()->getRequest()->getParam('id');
 $_product  = Mage::getModel('catalog/product')->load($productId);
 $reviews = Mage::getModel('review/review') ->getResourceCollection() 
 ->addStoreFilter(Mage::app()->getStore()->getId()) 
 ->addEntityFilter('product', $productId) 
 ->addStatusFilter(Mage_Review_Model_Review::STATUS_APPROVED) 
 ->setDateOrder() ->addRateVotes();
 $avg = 0; $avgper = 0; $totalrv = 0; $totalrvper =0; $ratings = array();       
 $count = 0;
 $star5 = 0; $star4 = 0; $star3 = 0; $star2 = 0; $star1 = 0;
 echo $review;
 if (count($reviews) > 0) { 
     foreach ($reviews->getItems() as $review) { 
         $mysum = 0;
         foreach( $review->getRatingVotes() as $vote ) { 
             $mysum = $mysum + ($vote->getPercent());
             $ratings[] = $vote->getPercent();
         } 
         $torat = $mysum/count($review->getRatingVotes());

         if ($torat <= 100 && $torat > 80) {
             $star5 = $star5 + 1;
         }
         if ($torat <= 80 && $torat > 60) {
             $star4 = $star4 + 1;
         }
         if ($torat <= 60 && $torat > 40) {
             $star3 = $star3 + 1;
         }
         if ($torat <= 40 && $torat > 20) {
             $star2 = $star2 + 1;
         }
         if ($torat <= 20 && $torat >= 0) {
             $star1 = $star1 + 1;
         } 
     } 
     $avg = array_sum($ratings)/count($ratings);
     //echo("<br>".$star5."<br>".$star4."<br>".$star3."<br>".$star2."
 <br>".$star1."<br>");
     $totalsum = $star5+$star4+$star3+$star2+$star1;
     //echo($totalsum);
     ?>
<div class="rating_main">
      <div class="rating_left">
      <table border="0" border-collapse="collapse" style="line-height: 13px;">
              <caption style="text-align:center; padding: 5px 0px;"><?php printf("%.2f", $avg*.05) ?> out of 5 
              <i class="fa fa-star fa-1" aria-hidden="true"></i></caption>
              <tr>
                  <td class="rating-table-margin">5 <i class="fa fa-star fa-1" aria-hidden="true"></i></td>
                  <td><div class="meter"><span style="width: <?php echo ((100/$totalsum)*$star5) ?>%"></span><p></p></div></td>
                  <td class="rating-table-margin"><?php echo $star5 ?></td>
              </tr>
              <tr>
                  <td class="rating-table-margin">4 <i class="fa fa-star fa-1" aria-hidden="true"></i></td>
                  <td><div class="meter"><span style="width: <?php echo ((100/$totalsum)*$star4) ?>%"></span><p></p></div></td>
                  <td class="rating-table-margin"><?php echo $star4 ?></td>
              </tr>
              <tr>
                  <td class="rating-table-margin">3 <i class="fa fa-star fa-1" aria-hidden="true"></i></td>
                  <td><div class="meter"><span style="width: <?php echo ((100/$totalsum)*$star3) ?>%"></span><p></p></div></td>
                  <td class="rating-table-margin"><?php echo $star3 ?></td>
              </tr>
              <tr>
                  <td class="rating-table-margin">2 <i class="fa fa-star fa-1" aria-hidden="true"></i></td>
                  <td><div class="meter"><span style="width: <?php echo ((100/$totalsum)*$star2) ?>%"></span><p></p></div></td>
                  <td class="rating-table-margin"><?php echo $star2 ?></td>
              </tr>
              <tr>
                  <td class="rating-table-margin">1 <i class="fa fa-star fa-1" aria-hidden="true"></i></td>
                  <td><div class="meter"><span style="width: <?php echo ((100/$totalsum)*$star1) ?>%; background-color: red;"></span><p></p></div></td>
                  <td class="rating-table-margin"><?php echo $star1 ?></td>
              </tr>              
      </table></div>
      <div>
        <div class="rating_right"><?php printf("%.2f", $avg*.05) ?><i class="fa fa-star fa-2" aria-hidden="true" style="font-size: 25px; position: relative; top: -3px;"></i>
        </div>
        <div style="float: left; padding-left: 4px; width: 47%; ">Total Review(s) : <?php echo($totalsum); ?>
        </div>
      </div>
    </div>
    <?php    
}
 ?>
 And also I used some css and jQuery and get this result

enter image description here

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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