Magento: Adding New option Most viewed or most popular option in sort by drop down in category list page in Magento
If you need to added a new sort by option like most viewed or most popular in drop down option then follow these 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
<?php class Yourmodule_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'); $this->removeOrderFromAvailableOrders('position'); $this->removeOrderFromAvailableOrders('relevance'); $this->setDefaultOrder('mostviewd'); $this->setDefaultDirection('asc'); $this->removeOrderFromAvailableOrders('entity_id'); krsort($this->_availableOrder); return $this->_availableOrder; } 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()); } if($this->getCurrentOrder()=='mostviewd') { $this->_collection->getSelect()-> 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); $this->_collection->getSelect()->reset()->from( array('e' =>new Zend_Db_Expr("({$sql})")), array('e' => "*") ); $limit= $this->getDefaultPerPageValue(); $page = $this->getRequest()->getParam('p'); if(isset($page)) if(($page - 1) == 0) $this->_collection->getSelect()->limit($limit); else $this->_collection->getSelect()->limit($limit, ($page - 1) * $limit); else $this->_collection->getSelect()->limit($limit); $this->_collection->load(); // $this->_collection->printlogquery(true); exit; return $this; } }
bookmarked!!, I really like your website!