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

Hi,

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



<?php
/**
pradeep.kumarrcs67@gmail.com

*/
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;
$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;
}


}


You can also check all attached file in

http://www.magentocommerce.com/boards/viewthread/273724/

No comments:

Post a Comment