MyPage is a personalized page based on your interests.The page is customized to help you to find content that matters you the most.


I'm not curious

How to Custom Sort Products by Quantity Sold on Magento?

Published on 28 April 15
682
0
3

How to Custom Sort Products by Quantity Sold on Magento? - Image 1
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

Letâs see how these overridden codes will appear. Check the code at

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.









































































































How to Custom Sort Products by Quantity Sold on Magento? - Image 1

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

0.1.0

Example_Catalog_Block_Product_List_Toolbar

Example_Catalog_Model_Config

Example_Catalog_Model_Resource_Product_Collection

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


Letâs see how these overridden codes will appear. Check the code at

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.

This blog is listed under Development & Implementations and E-Commerce Community

Related Posts:

Magento

 
Post a Comment

Please notify me the replies via email.

Important:
  • We hope the conversations that take place on MyTechLogy.com will be constructive and thought-provoking.
  • To ensure the quality of the discussion, our moderators may review/edit the comments for clarity and relevance.
  • Comments that are promotional, mean-spirited, or off-topic may be deleted per the moderators' judgment.
You may also be interested in
Awards & Accolades for MyTechLogy
Winner of
REDHERRING
Top 100 Asia
Finalist at SiTF Awards 2014 under the category Best Social & Community Product
Finalist at HR Vendor of the Year 2015 Awards under the category Best Learning Management System
Finalist at HR Vendor of the Year 2015 Awards under the category Best Talent Management Software
Hidden Image Url

Back to Top