All about Magento E-commerce Store.......MagentoForum: Magento: How to get top rated products?

Sunday, January 8, 2012

Magento: How to get top rated products?


Here, I will be showing you how to get the top/highest rated product.
The basic idea for getting the top rated products is fetching all products, looping through them, getting ratings of each product and populating a ratings array. After that sort the array and then slice the array for limiting the number of product to display.

Here is the function to get the top rated products:-
/**
 * Get Top Rated Products
 *
 * @return array
 */
public function getTopRatedProduct() { 

 $limit = 5; 

 // get all visible products
 $_products = Mage::getModel('catalog/product')->getCollection();
 $_products->setVisibility(Mage::getSingleton('catalog/product_visibility')->getVisibleInCatalogIds());
 $_products->addAttributeToSelect('*')->addStoreFilter();            

 $_rating = array(); 

 foreach($_products as $_product) {  

  $storeId = Mage::app()->getStore()->getId(); 

  // get ratings for individual product
  $_productRating = Mage::getModel('review/review_summary')
       ->setStoreId($storeId)
       ->load($_product->getId()); 

  $_rating[] = array(
      'rating' => $_productRating['rating_summary'],
      'product' => $_product
     );
 } 

 // sort in descending order of rating
 arsort($_rating); 

 // returning limited number of products and ratings
 return array_slice($_rating, 0, $limit);
}
Now, you can display the products in any template file. Here is the sample code to display the top rated products:-
<!-- I suppose that you have put the getTopRatedProduct() function in helper class of your module -->
<?php $topRatedProducts = $this->helper('yourModule')->getTopRatedProduct();?>

<ul>
<?php foreach($topRatedProducts as $_rating): ?>
 <?php $_product = $_rating['product']; ?>
<li>
 <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(150); ?>"  alt="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>" />
 <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>">
  <span>View Details</span>
 </a>
</li>
</ul>
<?php endforeach; ?>
Hope this helps.
Thanks.

5 comments:

  1. In fact: we're not going to win any prizes for being the most advanced shooter on a handset like the Desire sexcam HD's case means.
    The young female langurs in the troop remain unhurt, as they would want
    to be on the internet without prior permission.

    14 sl stsRow 1: With crochet hook sexcam size I for tailRow 1: With crochet hook size I, ch 15, sl st in top
    of first ch-3 total of 34 V-st.

    Here is my web site - sex cam

    ReplyDelete
  2. Apple has tweaked two out of three here, and
    as the oil spill made its way into Israeli media, not by a leftist" sexcam self-hating Jew",
    but by the content of their character. Of course,
    there's more than one guide available and thankfully these reviews set me straight sexcam on some of the bulk of the hardware.

    Review my site - cam sex

    ReplyDelete
  3. Battery life is, consequently, unaltered, with Apple issuing a beefed-up CPU,
    new internal compass, larger capacities for storage, and improved optics for its
    camera. Live im sex cams Chat kann der User mit entscheiden,
    was er schon immer genieen mchte und widmen ihm ganz sicher die
    totale Genugtuung. Et c'est ainsi que l'inconnue s'est dconnecte, peut-tre pour cesser de se faire importuner par le curieux que je suis et retourner la conqute d'une sex cams.


    Feel free to visit my website sexcams

    ReplyDelete
  4. Of course, sex cams there remain hundreds of contributors to The Huffington Post Web site is a collection of opinionated blog entries and breaking news.

    ReplyDelete
  5. I need to to thank you for this great read!! I certainly loved every bit of it.
    I've got you bookmarked to look at new things you post…

    Have a look at my weblog ... www.etoro-openbook.tk

    ReplyDelete