Setting Default Sort Order by Date in Magento
Sorting products by date is a common requirement for many e-commerce stores. While Magento allows you to sort products by various attributes including date (created_at), setting the default sort order can be tricky, especially when you want to set it to descending (DESC) order.
The Problem: Setting Default Sort Order in Magento
In your case, you’ve tried several methods to set the default sort order to ‘desc’ and use the created_at field. You’ve attempted this through catalog.xml:
<action method="setDefaultDirection"><dir>desc</dir></action>
<action method="setDefaultOrder"><field>created_at</field></action>
And you’ve also tried setting the direction in your Catalog/Block/Product/List/Toolbar.php file:
protected $_direction = 'desc';
However, these methods are not working as expected in Magento 1.7.0.2.
Understanding the Magento Sorting Mechanism
To understand why this isn’t working, let’s first look at how Magento handles product sorting. The sort order is determined by the Catalog/Block/Product/List/Toolbar.php file, which uses the setAvailableOrders method to define available sorting options:
public function getAvailableOrders()
{
if (!$this->isAllowed()) {
return false;
}
$availableOrders = $this->getConfig()->getAttributeUsedForSortByArray();
$result = array();
foreach ($availableOrders as $field => $label) {
$result[$field] = $this->escapeHtml($label);
}
return $result;
}
This method retrieves the available sorting options from the Config.php file. By default, these options are set in Mage/Catalog/Model/Config.php and do not include ‘created_at’:
public function getAttributeUsedForSortByArray()
{
return array(
'name' => $this->__('Name'),
'price' => $this->__('Price'),
);
}
Solving the Problem: Override the Config Model
To add ‘created_at’ to the available sorting options and set it as the default, you need to override the Mage_Catalog_Model_Config class. Create a new module in app/code/local/SortByDate/ with two files: Catalog/Model/Config.php and etc/config.xml.
In your local SortByDate/Catalog/Model/Config.php:
class SortByDate_Catalog_Model_Config extends Mage_Catalog_Model_Config {
public function getAttributeUsedForSortByArray()
{
$options = parent::getAttributeUsedForSortByArray();
if (!isset($options['created_at'])) {
$options['created_at'] = Mage::helper('catalog')->__('Date');
}
return $options;
}
}
Then, in your local SortByDate/etc/config.xml:
<config>
<global>
<models>
<catalog>
<rewrite>
<config>SortByDate_Catalog_Model_Config</config>
</rewrite>
</catalog>
</models>
</global>
</config>
This will add ‘created_at’ to the available sorting options and set it as ‘Date’.
Setting the Default Sort Field
However, this still doesn’t set ‘created_at’ as the default sort field. To do this, you need to modify Mage/Catalog/Block/Product/List/Toolbar.php:
public function getAvailableOrders()
{
if (!$this->isAllowed()) {
return false;
}
$availableOrders = $this->getConfig()->getAttributeUsedForSortByArray();
// Add this line
$defaultField = 'created_at';
$result = array();
foreach ($availableOrders as $field => $label) {
if ($field == $defaultField) {
$result[$field] = '<span class="current">' . $this->escapeHtml($label) . '</span>';
} else {
$result[$field] = $this->getUrl('*/*/index', array('_current' => true, 'sort' => $field)) . ' ' . $this->escapeHtml($label);
}
}
return $result;
}
This sets the default sort field to ‘created_at’.
Conclusion
Setting the default sort order by date in Magento requires a combination of overriding the Config model and modifying the Toolbar.php file. By following this guide, you should be able to add date as an available sorting option and set it as the default.
Remember that Magento 1 is no longer supported as of June 2020. For new projects, I highly recommend using Magento Commerce (Adobe Commerce) or Open Source (Magento Open Source), which offer advanced features like the Visual Merchandiser for easy product sorting.
If you’re looking to upgrade from an older version of Magento, our team at Laravel Company can help guide you through the process.