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
02 | Mage_Catalog_Model_Product_Visibility::VISIBILITY_BOTH, |
03 | Mage_Catalog_Model_Product_Visibility::VISIBILITY_IN_CATALOG |
06 | $_productCollection = Mage::getResourceModel( 'reports/product_collection' ) |
07 | ->addAttributeToSelect( '*' ) |
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 ): ?> |
05 | foreach ( explode ( "," , $product ->category_ids) as $catId ){ |
08 | $cat = Mage::getModel( 'catalog/category' ) |
09 | ->setStoreId(Mage::app()->getStore()->getId()) |
11 | $catName = $cat ->getName(); |
13 | $catLink = $cat ->getUrlPath(); |
14 | $categories .= '<a href="' . $catLink . '" title="' . $catName . '">' . $catName . '</a> ' ; |
19 | <?php if ( $counter <= $totalPerPage ): ?> |
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" />--> |
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 echo Mage::helper( 'core' )->currency( $product ->price) ?> </span> </p> |
35 | <br class = "spacer" /></div> |
37 | <h4><?php echo $product ->name ?></h4> |
39 | <small><?php echo $this ->__( 'Total soled quantities' ) ?>: <?php echo (int) $product ->ordered_qty ?></small><br /> |
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" /> |
45 | <?php echo $this ->__( 'Categories: ' ) ?><?php echo $categories ?> |
46 | <p><?php echo $product ->short_description ?></p> |
48 | <?php endif ; $counter ++; ?> |
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
3 | < reference name = "content" > |
5 | < strong > < block type = "catalog/product" name = "highsold" as = "highsold" template = "catalog/product/highsold.phtml" /></ strong > |
6 | < block type = "cms/page" name = "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!
Sorry but this is not clear at all. what code do I put in the highsold.phtml?
ReplyDeleteI think TPT could really use more resources on these topics. site
ReplyDelete