Magento Best Selling Products in homepage

Magento keeps tracks of the sold item , sometimes we need to displayed the most selled product in the homepage . So for that case i find the article while searching and i am using that article in this post.
Simply create directory as app/code/local/Mage/Catalog/Block/Product and create file Bestseller.php , and paste the following code.

class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract
    public function __construct()

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

        $products = Mage::getResourceModel('reports/product_collection')
            ->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description')) //edit to suit tastes
            ->setOrder('ordered_qty', 'desc'); //best sellers on top



Now we need a template to use. So for that case create the file bestseller.phtml in your design folder as following order,
app/design/frontend/*/*/template/catalog/product/bestseller.phtml . Then paste the following code

 <?php if (($_products = $this->getProductCollection()) && $_products->getSize()): ?>
<div class="home-page-cntr">
<?php $i=0; foreach ($_products->getItems() as $_product): ?>
    <?php if ($i>5): continue; endif; ?>

<div class="home-page-item">
        <div class="home-page-img">
            <a href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>">
                 <img src="<?php echo $this->helper('catalog/image')->init($_product, 'small_image')->resize(65,65); ?>" alt="<?php echo $this->htmlEscape($_product->getName()) ?>"/>
            <?php echo $_product->getDescription(); //also getShortDescription ?>
        <div class="home-page-txt">
            <p><a class="product-name" href="<?php echo $_product->getProductUrl() ?>" title="<?php echo $this->htmlEscape($_product->getName()) ?>)"><?php echo $_product->getName() ?></a></p>
            <?php //echo $this->helper('review/product')->getSummaryHtml($_product, 'short') //product review link ?>
            <?php echo $this->getReviewsSummaryHtml($_product, false, true)?>
            <?php //echo $this->helper('catalog/product')->getPriceHtml($_product) ?>
            <?php echo $this->getPriceHtml($_product) ?>
            <?php echo $_product->getProductId(); ?>
            <?php if($_product->getevent_date()) {echo $_product->getevent_date();} ?>
<?php $i++; endforeach; ?>
<?php for($i;$i%5!=0;$i++): ?>
    <?php endfor ?>
<?php endif; ?> 

To use this code, you need to add the block somewhere, either via XML or in a CMS page like your home page:
To use in a CMS page, use this where you enter your content – use it where you want to this item to show up!:

{{block type="catalog/product_bestseller"  template="catalog/product/bestseller.phtml"}}

Above code is tested and implemented in Magento

Source :

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.