Wednesday, September 5, 2012

Bestseller Products in Magento


>>  You can easily get the bestseller products using this code :

$storeId = Mage::app()->getStore()->getStoreId();
$sales_order = Mage::getSingleton('core/resource')->getTableName('sales_flat_order');
$sales_order_item = Mage::getSingleton('core/resource')->getTableName('sales_flat_order_item');

$_productCollection = Mage::getModel('sales/order_item')->getCollection();

$_productCollection = $_productCollection->addAttributeToSelect('order_id')
                ->addAttributeToSelect('product_id')
                ->addAttributeToFilter('main_table.store_id', $storeId)
                ->addAttributeToFilter('main_table.parent_item_id', array('null' => true));
               
$_productCollection->getSelect()
                   ->join(array('so' => $sales_order), 'main_table.order_id = so.entity_id',  array('so.*'))
                   ->where("so.status <> 'canceled'")
                   ->columns('SUM(main_table.qty_ordered) AS total_qty')
                   ->order(array('total_qty desc'))
                   ->group(array('main_table.product_id'));

foreach($_productCollection as $order)
{
    $_product = Mage::getModel('catalog/product')->load($order->getProductId());
    ...........................................................
    ............................................................// show product info here
}




                         ***********         Good Luck        **********