How to add sorting option – “Sort by Date” in Magento

You may want to provide a sorting option “Creation Date” so that customers can easily find the newest and the oldest items.

To do so extend the class “Mage_Catalog_Model_Config”  located at {{MAGENTO_ROOT}}/app/code/core/Mage/Catalog/Model/Config.php in a custom module and override the function “getAttributeUsedForSortByArray()” as shown below:


/**
* Retrieve Attributes Used for Sort by as array
* key = code, value = name
*
* @return array
*/
public function getAttributeUsedForSortByArray()
{
$options = array(
'position'  => Mage::helper('catalog')->__('Position'),
'created_at'  => Mage::helper('catalog')->__('Creation Date')
);
foreach ($this->getAttributesUsedForSortBy() as $attribute) {
/* @var $attribute Mage_Eav_Model_Entity_Attribute_Abstract */
$options[$attribute->getAttributeCode()] = $attribute->getStoreLabel();
}

return $options;
}

In the above function we have just added the below line to the Magento core class function:


'created_at'  => Mage::helper('catalog')->__('Creation Date')

After this change, you can see the “Creation Date” option in the  sort dropdown on the frontend.

 

In the Magento admin you have two places where the sorting options can be configured:

  1. System >> Configuration >> Catalog >> Frontend >> Product Listing Sort by dropdown
  2. Catalog >> Manage Categories >> Display Settings tab for a category

1. System >> Configuration >> Catalog >> Frontend >> Product Listing Sort by dropdown

For adding the “Creation Date” option in this screen extend the class “Mage_Adminhtml_Model_System_Config_Source_Catalog_ListSort” located at {{MAGENTO_ROOT}}/app/code/core/Mage/Adminhtml/Model/System/Config/Source/Catalog/ListSort.php as shown below:


/**
* Retrieve option values array
*
* @return array
*/
public function toOptionArray()
{
$options = array();
$options[] = array(
'label' => Mage::helper('catalog')->__('Best Value'),
'value' => 'position'
);
$options[] = array(
'label' => Mage::helper('catalog')->__('Creation Date'),
'value' => 'created_at'
);
foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {
$options[] = array(
'label' => Mage::helper('catalog')->__($attribute['frontend_label']),
'value' => $attribute['attribute_code']
);
}
return $options;
}

2. Catalog >> Manage Categories >> Display Settings tab for a category

For adding the “Creation Date” option in this screen extend the class “Mage_Catalog_Model_Category_Attribute_Source_Sortby” located at {{MAGENTO_ROOT}}/app/code/core/Mage/Catalog/Model/Category/Attribute/Source/Sortby.php as shown below:


/**
* Retrieve All options
*
* @return array
*/
public function getAllOptions()
{
if (is_null($this->_options)) {
$this->_options = array(array(
'label' => Mage::helper('catalog')->__('Best Value'),
'value' => 'position'
),
array(
'label' => Mage::helper('catalog')->__('Creation Date'),
'value' => 'created_at'
)
);
foreach ($this->_getCatalogConfig()->getAttributesUsedForSortBy() as $attribute) {
$this->_options[] = array(
'label' => Mage::helper('catalog')->__($attribute['frontend_label']),
'value' => $attribute['attribute_code']
);
}
}
return $this->_options;
}

 

Alternate Method for adding “Creation Date” in Sort dropdown:

Alternatively if you do not want to override files by creating a custom module then you can make changes directly in Magento database to add the “Creation Date” option in sorting dropdown as explained below:

1. Find the `entity_type_id` from the table ‘eav_entity_type’.


SELECT `entity_type_id` FROM `eav_entity_type` WHERE `entity_type_code`='catalog_product';

Its comes out to be 10 in our case.

2. Find the `attribute_id` from the table ‘eav_attribute’.


SELECT * FROM `eav_attribute` WHERE `entity_type_id` = 10 AND `attribute_code` LIKE 'created_at';

The attribute_id comes out to be 932.

3.  Set `used_for_sort_by`=1 for `attribute_id`=932 in table ‘catalog_eav_attribute’.


UPDATE `catalog_eav_attribute` SET `used_for_sort_by`=1  WHERE `attribute_id`=932;

4. Update the `frontend_label`  for `attribute_id`=932 and `attribute_code`=’created_at’ in table ‘eav_attribute’


UPDATE `eav_attribute` SET `frontend_label`='Creation Date' WHERE `attribute_id`=932 and `attribute_code`='created_at';

That’s all, the “Creation Date” option is added in the sort dropdown on the frontend as well as backed(admin).

 

The following code in file {{MAGENTO_ROOT}}/app/design/frontend/base/default/template/catalog/product/list/toolbar.phtml is used to display the sorting dropdown on the frontend.


<div class="sort-by">
<label><?php echo $this->__('Sort By') ?></label>
<select onchange="setLocation(this.value)">
<?php foreach($this->getAvailableOrders() as $_key=>$_order): ?>
<option value="<?php echo $this->getOrderUrl($_key, 'asc') ?>"<?php if($this->isOrderCurrent($_key)): ?> selected="selected"<?php endif; ?>>
<?php echo $this->__($_order) ?>
</option>
<?php endforeach; ?>
</select>
<?php if($this->getCurrentDirection() == 'desc'): ?>
<a href="<?php echo $this->getOrderUrl(null, 'asc') ?>" title="<?php echo $this->__('Set Ascending Direction') ?>"><img src="<?php echo $this->getSkinUrl('images/i_desc_arrow.gif') ?>" alt="<?php echo $this->__('Set Ascending Direction') ?>" class="v-middle" /></a>
<?php else: ?>
<a href="<?php echo $this->getOrderUrl(null, 'desc') ?>" title="<?php echo $this->__('Set Descending Direction') ?>"><img src="<?php echo $this->getSkinUrl('images/i_asc_arrow.gif') ?>" alt="<?php echo $this->__('Set Descending Direction') ?>" class="v-middle" /></a>
<?php endif; ?>
</div>

You can override this template file as per you theme requirements.

 

By default Magento does sorting in “ascending” order. So, in order to change the default sort order direction to “descending”  you need to extend the class “Mage_Catalog_Block_Product_List_Toolbar” located at {{MAGENTO_ROOT}}/app/code/core/Mage/Catalog/Block/Product/List/Toolbar.php and add the following lines in this file:


/**
* Default direction
*
* @var string
*/
protected $_direction           = 'desc';

Leave a Comment

Back to top