How can i add a custom sort to sort products in a category using the name and price both?
1 Answer
As you want multiple sort then you need to rewrite class Mage_Catalog_Block_Product_List_Toolbar
bcoz magento set it's product listing page sort order from setCollection()
function.
So add this code:
->setOrder('name','DESC')->setOrder('name','DESC')
Rewrite class
<?php
class [ModuleNameSpace]_[ModuleName]_Block_Catalog_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar
{
public function setCollection($collection)
{
$this->_collection = $collection;
$this->_collection->setCurPage($this->getCurrentPage());
// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
$this->_collection->setPageSize($limit);
}
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
}
// add this
$this->_collection->setOrder('name','DESC')->setOrder('name','DESC');
return $this;
}
}
Rewrite code at config.xml
<global>
<blocks>
[.....]
<catalog>
<rewrite>
<product_list_toolbar>[ModuleNameSpace]_[ModuleName]_Block_Catalog_Product_List_Toolbar</product_list_toolbar>
</rewrite>
</catalog>
[.....]
</blocks>
</global>
-
Thanks for the answer i will try this out and mark this answer as correct. Commented Jun 9, 2016 at 11:26
-
Sorry this method doesn't work. I tried with
->setOrder('name','DESC')->setOrder('price','DESC')
still what it will do is first it will sort according to name and then again it will sort the list according to price which the final result will be sort only according to the price. Commented Jun 12, 2016 at 11:40