How to Custom Sort Products by Quantity Sold on Magento?

Custom sort option is available with Magento. Most often, you would find options readily available to sort products by their position, price or even name. What if you want to show how many times these products has been sold? Here you will learn how to sort products by the number of times these products have been sold.

Instead of modifying the core files, it is a good practice to create a new module that offers the same functionality and can be upgraded easily from your end.

Follow this path app/etc/modules/Example_Catalog.xml, and create your own module. Use this module to create your own custom config.xml file and place it along the following path: Example/Catalog/etc/config.xml





























The three files that are overridden in this particular code are:

  • Mage_Catalog_Block_Product_List_Toolbar
  • Mage_Catalog_Model_Config
  • Mage_Catalog_Model_Resource_Product_Collection

The three files that are overridden in this particular code are:


The code will appear as follows

class Example_Catalog_Block_Product_List_Toolbar extends



public function setCollection($collection)



if ($this->getCurrentOrder()) {

if($this->getCurrentOrder() == ‘qty_ordered’) {


array(‘sfoi’ => $collection->getResource()->getTable(‘sales/order_item’)),

‘e.entity_id = sfoi.product_id’,

array(‘qty_ordered’ => ‘SUM(sfoi.qty_ordered)’)



->order(‘qty_ordered ‘ . $this->getCurrentDirection());

} else {


->setOrder($this->getCurrentOrder(), $this->getCurrentDirection())->getSelect();



return $this;



While as part of this technique we will be extending the properties and methods of Mage_Catalog_Block_Product_List_Toolbar to the newly created module, we will be overriding the contents present within setCollection() method with custom properties and methods that have been defined for the new module.

The coding for Example_Catalog_Model_Config is as follows

class Example_Catalog_Model_Config extends Mage_Catalog_Model_Config


public function getAttributeUsedForSortByArray()


return array_merge(


array(‘qty_ordered’ => Mage::helper(‘catalog’)->__(‘Sold quantity’))




With this code you have implemented the sorting using quantity sold as the function. But, the issue of pagination remains to be tackled. You can programmatically solve this issue. Go to Example/Catalog/Model/Resource/Product/Collection.php

With the code given below pasted to the above mentioned location, you can tackle the issue.

class Example_Catalog_Model_Resource_Product_Collection extends Mage_Catalog_Model_Resource_Product_Collection


protected function _getSelectCountSql($select = null, $resetLeftJoins = true)



$countSelect = (is_null($select)) ?

$this->_getClearSelect() :


if(count($countSelect->getPart(Zend_Db_Select::GROUP)) > 0) {



$countSelect->columns(‘COUNT(DISTINCT e.entity_id)’);

if ($resetLeftJoins) {



return $countSelect;



Now, with the pagination issue solved, you are ready to sort the products on your store by the quantity sold.

Deepa, a technical writer with Semaphore Software, who now devotes her time in advising its clients to hire Dedicated Magento Developers. Her love for reading helps her constantly provide latest information on different technical and design aspects of Magento

About deeparanganathan