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

<config>

<modules>

<Example_Catalog>

<version>0.1.0</version>

</Example_Catalog>

</modules>

<global>

<blocks>

<catalog>

<rewrite>

<product_list_toolbar>Example_Catalog_Block_Product_List_Toolbar</product_list_toolbar>

</rewrite>

</catalog>

</blocks>

<models>

<catalog>

<rewrite>

<config>Example_Catalog_Model_Config</config>

</rewrite>

</catalog>

<catalog_resource>

<rewrite>

<product_collection>Example_Catalog_Model_Resource_Product_Collection</product_collection>

</rewrite>

</catalog_resource>

</models>

</global>

</config>

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:

app/code/local/Example_Catalog_Block_Product_List_Toolbar

The code will appear as follows

class Example_Catalog_Block_Product_List_Toolbar extends

Mage_Catalog_Block_Product_List_Toolbar

{

public function setCollection($collection)

{

parent::setCollection($collection);

if ($this->getCurrentOrder()) {

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

$this->getCollection()->getSelect()->joinLeft(

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

‘e.entity_id = sfoi.product_id’,

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

)

->group(‘e.entity_id’)

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

} else {

$this->getCollection()

->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(

parent::getAttributeUsedForSortByArray(),

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)

{

$this->_renderFilters();

$countSelect = (is_null($select)) ?

$this->_getClearSelect() :

$this->_buildClearSelect($select);

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

$countSelect->reset(Zend_Db_Select::GROUP);

}

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

if ($resetLeftJoins) {

$countSelect->resetJoinLeft();

}

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