Thursday 18 April 2013

Show best selling product in magento

Want to display the best selling products in your Magento store on the frontpage or anywhere else in your store? The best selling products means the products sold in highest quantity in Ascending order. This functionality is for some strange reason not included in Magento by default so we’ll explain how you can set it up yourself.
Main logic for this is we have to get the product list in ascending order by [order_qty]. So to get this type of list we have to apply
01$visibility = array(
02Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH,
03Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG
04);
05 
06$_productCollection = Mage::getResourceModel('reports/product_collection')
07->addAttributeToSelect('*')
08->addOrderedQty()
09->addAttributeToFilter('visibility', $visibility)
10->setOrder('ordered_qty', 'desc');
By this way you get the complete list of product order accoding to order quantity for particular product.
Now to display that list in well formated manner you have to loop over the array of product as
01<?php foreach($_productCollection as $product): ?>
02 
03<?php
04$categories = null;
05foreach (explode(",", $product->category_ids) as $catId){
06 
07//Mage_Catalog_Model_Category
08$cat = Mage::getModel('catalog/category')
09->setStoreId(Mage::app()->getStore()->getId())
10->load($catId);
11$catName = $cat->getName();
12 
13$catLink = $cat->getUrlPath();
14$categories .= '<a href="'.$catLink.'" title="'.$catName.'">'.$catName.'</a>&nbsp;&nbsp;';
15}
16 
17?>
18 
19<?php if($counter <= $totalPerPage): ?>
20 
21<?php $productUrl  $product->getProductUrl() ?>
22<div class="best-sellh">
23<div class="wraptocenter">
24<a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
25<img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(120); ?>" alt="Product image"  class="shadow" rel="black" />
26<!--        <img src="images/prodimg01.jpg" alt="chrysler-building" height="150"width="100" class="shadow" rel="black"/>-->
27</a>
28</div>
29 
30<div class="img_txt" >
31<span class="yellow-bg-text"><?php echo $product->name ?></span> <?=$catName?><br />
32<p class="price_txt">starts from <span class="price_hd"><?php echoMage::helper('core')->currency($product->price) ?> </span> </p>
33</div>
34 
35<br class="spacer" /></div>
36<!--
37<h4><?php echo $product->name ?></h4>
38</a>
39<small><?php echo $this->__('Total soled quantities') ?>: <?php echo(int)$product->ordered_qty ?></small><br />
40 
41<a href="<?php echo $productUrl ?>" title="View <?php echo $product->name ?>">
42<img src="<?php echo $this->helper('catalog/image')->init($product, 'image')->resize(120); ?>" alt="Product image"  />
43</a> <br />
44 
45<?php echo $this->__('Categories: ') ?><?php echo $categories ?>
46<p><?php echo $product->short_description ?></p>
47-->
48<?php endif; $counter++; ?>
49<?php endforeach; ?>
In this way you can get the list of best selling product.
How to use this code:
  • Just create one phtml file as[highsold.phtml] in category/product directory
  • Paste this code in that file [highsold.phtml]
  • Now go to layout/cms.xml file and add following line of code
1<cms_page>
2 
3<reference name="content">
4 
5<strong> <block type="catalog/product" name="highsold" as="highsold"template="catalog/product/highsold.phtml" /></strong>
6<block type="cms/page" name="cms_page" />
7</reference>
8</cms_page>
By this way you are creating a block named as highsold
  • Now if you want to display the product list on home page.Just go to home.phtml file and ad following lines as<?php echo $this->getChildHtml(‘highsold’) ?>
  • next go to admin/cms/manage pages/ and select home pagel. In content text area add following lines as
1{{block type="core/template" name="default_home_page" template="cms/default/home.phtml" }}
Now you should see the products perfectly displayed on home page!

2 comments:

  1. Sorry but this is not clear at all. what code do I put in the highsold.phtml?

    ReplyDelete
  2. I think TPT could really use more resources on these topics. site

    ReplyDelete