You get a competitive edge when you enable sort by quantity sold on your frontend. Your customers get an idea on the popularity of the product and they can check on the reviews of these popular products. Imbibe this technique to your Magento store and gain more conversions. If you have popular products, your store is going to gain popularity too!
Good Stuff Happens in 1:1 Meetings: Why you need them and how to do them well
How to Custom Sort Products by Quantity Sold on Magento?
1. 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>
3. 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()
4. 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) {
5. $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.
Conclusion
You get a competitive edge when you enable sort by quantity sold on your
frontend. Your customers get an idea on the popularity of the product and
they can check on the reviews of these popular products. Imbibe this
technique to your Magento store and gain more conversions. If you have
popular products, your store is going to gain popularity too!
Original Source:
http://www.mytechlogy.com/IT-blogs/7515/how-to-custom-sort-
products-by-quantity-sold-on-magento/
6. $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.
Conclusion
You get a competitive edge when you enable sort by quantity sold on your
frontend. Your customers get an idea on the popularity of the product and
they can check on the reviews of these popular products. Imbibe this
technique to your Magento store and gain more conversions. If you have
popular products, your store is going to gain popularity too!
Original Source:
http://www.mytechlogy.com/IT-blogs/7515/how-to-custom-sort-
products-by-quantity-sold-on-magento/