Custom ‘Sort by’ drop-down menu for Magento
December 14th, 2011
Yesterday i have spent some hours getting this done, so here the explanation hoping to save you some time.
Magento version 1.6.0
Modified core files:
/app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php
Modified template files:
/app/design/frontend/mytemplate/default/template/catalog/product/list/toolbar.phtml
/app/design/frontend/mytemplate/default/template/catalog/layer/view.phtml
Translations:
/app/design/frontend/mytemplate/default/locale/it_IT/translate.csv
Life-Saving Threads: where i have found the hints to make this work
http://www.magentocommerce.com/boards/viewthread/20437/
http://www.magentocommerce.com/boards/&/viewthread/4029/P15/
First you have to setup the new order options in Toolbar.php.
Find the function named getAvailableOrders and modify it like this:
/**
* Retrieve available Order fields list
*
* @return array
*/
public function getAvailableOrders()
{
// SLD CUSTOM 13.12.2011
$this->_availableOrder = array(
'position' => $this->__('Popularity'),
'entity_id' => $this->__('Latest arrivals'),
'name' => $this->__('Name'),
'price' => $this->__('Price')
);
// / SLD CUSTOM
return $this->_availableOrder;
}
now we can order our products by position, entity_id, name and price.
Now in our toolbar.phtml we can use those options to modify the “Sort By” drop-down menu. Here we use our new options with getOrderUrl and directions ( ‘asc’/'desc’ ) to get values for our drop-down menu.
example: “order by lowest price first url” is < ?php echo $ob->getOrderUrl(’price’, ‘asc’); ?>
< ?php $ob = new Mage_Catalog_Block_Product_List_Toolbar;?>
<fieldset class="sort-by">
<select onchange="setLocation(this.value)">
<option value="< ?php echo $ob->getOrderUrl('position', 'asc') ?>"< ?php if($ob->isOrderCurrent('position') && $ob->getCurrentDirection() == 'asc'): ?> selected="selected"< ?php endif; ?>>
< ?php echo $this->__('Popularity'); ?>
<option value="< ?php echo $ob->getOrderUrl('price', 'asc') ?>"< ?php if($ob->isOrderCurrent('price') && $ob->getCurrentDirection() == 'asc'): ?> selected="selected"< ?php endif; ?>>
< ?php echo $this->__('Lowest price'); ?>
<option value="< ?php echo $ob->getOrderUrl('price', 'desc') ?>"< ?php if($ob->isOrderCurrent('price') && $ob->getCurrentDirection() == 'desc'): ?> selected="selected"< ?php endif; ?>>
< ?php echo $this->__('Highest price'); ?>
<option value="< ?php echo $ob->getOrderUrl('entity_id', 'desc') ?>"< ?php if($ob->isOrderCurrent('entity_id') && $ob->getCurrentDirection() == 'desc'): ?> selected="selected"< ?php endif; ?>>
< ?php echo $this->__('Latest arrivals'); ?>
Leave a Reply