Sunday, September 29, 2013

Bestseller products in Magento

 Using query from database

SELECT `e`.*, `bs`.* FROM `catalog_product_entity` AS `e` INNER JOIN (SELECT `sales_flat_order_item`.`product_id`, SUM(`qty_ordered`) AS `count` FROM `sales_flat_order_item` GROUP BY `product_id`) AS `bs` ON bs.product_id = e.entity_id ORDER BY `bs`.`count` DESC;

And using the collection

$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);



1 comment: