Sunday, May 20, 2012

Magento Best seller and Top seller

To show bestseller at Top seller at home page.so that best products should come on home page.For that follow the following steps
Create a Bestseller.php file and put it here :



app/code/local/Mage/Catalog/Block/Product/Bestseller.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('id')
            ->addAttributeToSelect(array('name', 'price', 'small_image'))
            ->setStoreId($storeId)
            ->addStoreFilter($storeId)
            ->setOrder('ordered_qty', 'desc'); // most best sellers on top
        Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($products);
        Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($products);
        $products->setPageSize(3)->setCurPage(1);
        $this->setProductCollection($products);
    }
}

Create bestseller.phtml file and put it here :
app/design/frontend/yourtheme/template/catalog/product/bestseller.phtml

<?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; ?>

now put this line where you want to view best selling products..
you can use through block or through XML also
{{block type="catalog/product_bestseller" template="catalog/product/bestseller.phtml"}}

<block type="catalog/product_bestseller" name="bestseller" template="catalog/product/bestseller.phtml">

Hope this will help you!

1 comment:

  1. hello
    thank you for the post.
    and what about if to use the extension for such case. I use magneto extension such as http://amasty.com/improved-sorting.html.
    In my point of view it is very powerful tool for magento. extremely expand basic magento functionality.

    ReplyDelete

Thankyou for your Comments

LinkWithin

Blog has moved, searching new blog...