Tuesday, February 21, 2012

Adding New option in catgeory sort by OR Adding Most viewed or most popluar option for sort by in category list page in Magento


If you need to added new sort by option like most viewed etc.. follow the steps

override app\code\core\Mage\Catalog\Block\Product\List\Toolbar.php

in local folder and add or override only this 2 functions
Replace default Toolbar.php code by this code (be care full on class name it depends on your module name )

I took ex;- Most Viewed


class Custommodule_Catalog_Block_Product_List_Toolbar extends Mage_Catalog_Block_Product_List_Toolbar

* Retrieve available Order fields list
* @return array
public function getAvailableOrders()

$this->addOrderToAvailableOrders('mostviewd', 'Most Popular');

//echo $this->getCurrentOrder(); exit;
return $this->_availableOrder;

public function setCollection($collection)
$this->_collection = $collection;


// we need to set pagination only if passed value integer and more that 0
$limit = (int)$this->getLimit();
if ($limit) {
if ($this->getCurrentOrder()) {
$this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
if($this->getCurrentOrder()=='mostviewd') {
joinInner('report_event AS _table_views',
' _table_views.object_id = e.entity_id',
'COUNT(_table_views.event_id) AS views')->
group('e.entity_id')->order('views DESC');

$sql = $this->_collection->getSelectSql(true);

array('e' =>new Zend_Db_Expr("({$sql})")),
array('e' => "*")
$limit= $this->getDefaultPerPageValue();
$page = $this->getRequest()->getParam('p');

if(($page - 1) == 0)
$this->_collection->getSelect()->limit($limit, ($page - 1) * $limit);


// $this->_collection->printlogquery(true); exit;
return $this;


You can also check all attached file in


No comments:

Post a Comment