The Situation
Recently, I had the pleasure of working on an existing site we took over. The site had been upgraded from Magento CE to EE before it came to me. One of the tasks was to make a new enterprise theme and clean up any unused or incompatible extensions.
I will just focus on one extension here that was added to allow the catalog to be sorted by newest products. This is the EWTechnologies_SortByNewest. It came to my attention after debugging the toolbar for not showing the pagination links where there is more than one page of products. So I looked into it and to my horror realized that it was overriding the default Mage_Catalog_Block_Product_List_Toolbar class but extending from Mage_Page_Block_Html_Pager. If that wasn’t bad enough, it was also completely overriding the Mage_Eav_Model_Config class. This one has been substantially changed in later versions of Magento so the override implements very old code. I decided that this is too risky to keep even though there were no other visible adverse effects. It certainly isn’t best practice to run outdated code in any case.
I thought this feature shouldn’t require overriding so much core code so I investigated and, to my delight, found that the solution was very simple and quite elegant and didn’t involve overriding any core code as such.
The Approach
The Product Entity in Magento has a ‘created_at’ attribute which stores the date when the product was created. Logically, it lends itself to be used for the sort by newest feature as it is set only once by the system, when the product was created.
A look into the eav_attribute table gives us the attribute_id and an inspection of the catalog_eav_attribute table shows that the attribute is set to not be used in the sort by drop-down. So, the obvious course of action was to set the attribute’s used_in_sort_by field to 1 and then the only other thing left was to update the frontend_label in eav_attribute to read “Newest”. That’s all, folks!
The Extension
I’ve whipped up a quick extensions that will perform those updates and makes it easy to install on any site that needs this feature.
Note: This should work fine for Magento CE 1.4.x and up, PE, EE 1.8.0 and up. There is no guarantee and I don’t provide support for this extension. Use at your own risk and ALWAYS backup your database before installing.
Conclusion
This approach should be upgrade safe and works on any Magento CE version over 1.4 as well as PE and EE versions. The only issue would be if a future Magento upgrade was updating the created_at attribute but then it’s easy enough to reapply the database edits to restore functionality – easier than troubleshoot issues with core code overrides.
Update: If you want to set the default sort direction for the toolbar, you can do this via the layout XML. Best in your local.xml but you can edit catalog.xml if your theme has overridden that:
<catalog_category_default>
/* other layout nodes */
<reference name="product_list_toolbar">
<action method="setDefaultDirection"><dir>desc</dir></action>
</reference>
</catalog_category_default>
<catalog_category_layered>
/* other layout nodes */
<reference name="product_list_toolbar">
<action method="setDefaultDirection"><dir>desc</dir></action>
</reference>
</catalog_category_layered>
Originally published on magebase.com. Copyright © 2011 Magebase - All Rights Reserved.




Proud members of the 









Why did I not think of that… Thanks
hi robert , great simple little extension, thankyou ! … except .. it defaults to sorting in the wrong direction .. oldest at the top … maybe im being dumb , but i cant find a way of setting it to sort the other way up !! guy
Thank you for the extension. It worked in CE 1.6.
To default newest to oldest (descending direction), change Toolbar.php about line 119 from ‘asc’ to ‘desc’
/** * Default direction * * @var string */ protected $_direction = 'desc';A better way of setting the default direction for the product listing is via layout xml, either in your local.xml or the catalog.xml if you have overridden that:
<catalog_category_default> /* other layout nodes */ <reference name="product_list_toolbar"> <action method="setDefaultDirection"><dir>desc</dir></action> </reference> </catalog_category_default> <catalog_category_layered> /* other layout nodes */ <reference name="product_list_toolbar"> <action method="setDefaultDirection"><dir>desc</dir></action> </reference> </catalog_category_layered>It is good practice not to “hack” core files. If a core php file must be changed, then proper extension override techniques should be applied.
Hey Robert,
sorry for the dumb question. But what is the best way to install the extension.
Load the files in the folders and then
call http://www.myshop/app/code/local/Magebase/SortbyNewest/sql/sortbynewest_setup/mysql4-install-0.1.0.php
or how it is done?
Greeetings
Jay
Hi Jay, you can do that rather manually in admin interface instead of installing the app. But if you want still install the app just upload everything to your Magento folder and that is it – on first load Magento will install it for you.
Robert, is there any posibility that I do not have created_at attribute? I cannot find it in admin and nor in mysql database.
@elvar
What Magento version are you using? It is possible that older versions did not have that DB field in the
catalog_product_entitytable.Thanks man. I found it in database eventually. What confused me probably is that it is not still exists in backend…
I also removed the Position attribute by this method.
i also tried to change the order of generating list of sort by (I have it 3 now) in mysql table (there is a column Poistion) but this does not affect the toolbar.
Do you know how to reorder the attributes for the sort by?
If you really want to make the created_at attribute appear in the admin, then you need to set the is_visible field in the catalog_eav_attribute table to ’1′ for that attribute (find the attribute_id in eav_attribute).
As far as the position in the filter drop-down goes, I think there is little you can do. The drop-down is output in the magento block class
Mage_Catalog_Block_Product_List_Toolbar::getAvailableOrders()but the available orders are set inMage_Catalog_Model_Config::getAttributeUsedForSortByArray()You will see that the “Position” attribute is added as hard-coded in the first place there but the other attributes are retrieved in an arbitrary order. So, to change the order, you could override the Config class method and do your custom method or, if you don’t want to override the core code, you could create a JavaScript function that will re-order the drop-down entries in the front-end after the page has loaded and include the JS on your catalog pages.
Well. I found that changing the ‘position’ column in database does not affect the toolbar but affect default sorting
I removed the sort by of ‘position’ type with $this->removeOrderFromAvailableOrders(‘position’);
Anyway – this is enough I can live with default order or sortby listing in toolbar.. Thank you I have lerned a lot!