Friday, 8 July 2011

how to get best selling product in magento?

Best Seller is a common module of an eCommerce solution. but it’s not a default module in magento.
u want to display the best selling products in your Magento store on the frontpage or anywhere else in your store? i found this snippets @MagentoForum, it’s easy to use…

[Step 1] 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('*')
            ->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);
    }
}

[Step 2] Create a bestseller.phtml file and put it here :
app/design/frontend/*/*/template/catalog/product/bestseller.phtml

getProductCollection()) && $_products->getSize()): ?>

__('Best Seller Products') ?>

getItems()) ?> getItems() as $_product): ?>
>


htmlEscape($_product->getName()) ?>


<?php echo $this->htmlEscape($this->getImageLabel($_product, 'small_image')) ?>

getRatingSummary()): ?>
getReviewsSummaryHtml($_product, 'short') ?>

getPriceHtml($_product, true) ?>
isSaleable()): ?>


__('Out of stock') ?>


getevent_date()) {echo $_product->getevent_date();} ?>

[Step 3] now put this line where you want to view best selling products..

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

Source : http://harisur.net/how-to-get-best-selling-product-in-magento

1 comment: