Magento: Get Best Selling products by category and date time

This article will show you how to get best selling products. You will see how to get overall best selling products as well as best selling products by category and by date time range.

Here is the code:-

Get overall Bestselling products


public function getBestsellingProducts()
{ 	
	// number of products to display
	$productCount = 5; 
	
	// store ID
	$storeId    = Mage::app()->getStore()->getId();       
	
	// get most viewed products for current category
	$products = Mage::getResourceModel('reports/product_collection')
					->addAttributeToSelect('*')		
					->addOrderedQty()
					->setStoreId($storeId)
					->addStoreFilter($storeId)					
					->setOrder('ordered_qty', 'desc')
					->setPageSize($productCount);
	
	Mage::getSingleton('catalog/product_status')
			->addVisibleFilterToCollection($products);
	Mage::getSingleton('catalog/product_visibility')
			->addVisibleInCatalogFilterToCollection($products);
	
	return $products; 
}

Get Bestselling products for current category


public function getBestsellingProducts()
{ 	
	// number of products to display
	$productCount = 5; 
	
	// store ID
	$storeId    = Mage::app()->getStore()->getId();       
	
	// get most viewed products for current category
	$products = Mage::getResourceModel('reports/product_collection')
					->addAttributeToSelect('*')		
					->addOrderedQty()
					->setStoreId($storeId)
					->addStoreFilter($storeId)
					->addCategoryFilter(Mage::registry('current_category'))
					->setOrder('ordered_qty', 'desc')
					->setPageSize($productCount);					 
	
	Mage::getSingleton('catalog/product_status')
			->addVisibleFilterToCollection($products);
	Mage::getSingleton('catalog/product_visibility')
			->addVisibleInCatalogFilterToCollection($products);
	
	return $products; 
}

Get Bestselling products for last 30 days


public function getBestsellingProducts()
{ 	
	// number of products to display
	$productCount = 5; 
	
	// store ID
	$storeId    = Mage::app()->getStore()->getId();

	// get today and last 30 days time
	$today = time();
	$last = $today - (60*60*24*30);

	$from = date("Y-m-d", $last);
	$to = date("Y-m-d", $today);
	
	// get most viewed products for current category
	$products = Mage::getResourceModel('reports/product_collection')
					->addAttributeToSelect('*')		
					->addOrderedQty($from, $to)
					->setStoreId($storeId)
					->addStoreFilter($storeId)					
					->setOrder('ordered_qty', 'desc')
					->setPageSize($productCount); 
	
	Mage::getSingleton('catalog/product_status')
			->addVisibleFilterToCollection($products);
	Mage::getSingleton('catalog/product_visibility')
			->addVisibleInCatalogFilterToCollection($products);
	
	return $products; 
}

Note: For Magento version 1.5 and higher, you can also look into the following files to get bestselling products:

– app/code/core/Mage/Sales/Model/Mysql4/Report/Bestsellers.php
– app/code/core/Mage/Sales/Model/Mysql4/Report/Bestsellers/Collection.php

Hope this helps. Thanks.