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.

 <?php
class Mage_Catalog_Block_Product_Bestseller extends Mage_Catalog_Block_Product_Abstract
{
    public function __construct()
    {
        parent::__construct();

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

        $products = Mage::getResourceModel('reports/product_collection')
            ->addOrderedQty()
            //->addAttributeToSelect('*')
            ->addAttributeToSelect(array('name', 'price', 'small_image', 'short_description', 'description')) //edit to suit tastes
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setOrder('ordered_qty', 'desc'); //best sellers on top
        
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);

        //$products->setPageSize(6)->setCurPage(1);

        $this->setProductCollection($products);
    }
} 

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()) ?>"/>
            </a>
            <?php echo $_product->getDescription(); //also getShortDescription ?>
        </div>
        <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();} ?>
            
</div>
</div>
<?php $i++; endforeach; ?>
<?php for($i;$i%5!=0;$i++): ?>
    <?php endfor ?>
</div>
<?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 1.7.0.2

Source : http://www.magentocommerce.com/boards/viewthread/14764/#t51479
http://www.magentocommerce.com/boards/viewthread/14764/#t78979

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.