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.
Where is Toolbar.php ??? If it so, I can use attribute “News from” to sort products in my categories.
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!
Perhaps there’s an event that could be used in order the sort the array?
Way better than overriding.
Hi Robert,
It might be a long shot but is it possible to add stock status to the front end sorting the same way? I dug through the eav_attribute table to no result and from a bit of research it looks like there is only product status available which is not much of use if the store is set to display out of stock products.
I’d be very grateful if you could let me know the above or a hint to an alternative solution.
Thanks in advance
Peter
It works fine on Magento 1.6.2 !
Thank you very much, i was working on since two days… And no way !
Hello. Thanks, it’s very useful and important information. We have an article on the similar topic posted here http://www.atwix.com/magento/custom-sorting-product-listing/ you’re welcome to check it out and leave your feedback.
Thanks for posting this extention. It sounds like just what I need. I am having no luck getting it to work on CE 1.7.0.2. I added the extension and it’s sorting in ascending order. My theme didn’t have a local.xml file in app/design/frontend/default/mytheme/layout. Should I be able to just create a new one and add it there? I tried that with your code and also tried adding the code to catalog.xml to change the sort order to descending but no luck.
tyishop,
Ii’m in 1.7.02
Yes just create a local.xml. I put it in app/design/frontend/default/default/layout
Is it possible to make this “Sort by newest” sort become the default sort when the page is opened?
i have the sort working and defaulting to desc in local.xml thanks
Set ‘newest’ in your config in System > Configuration > Catalog / Catalog > Frontend > Product Listing Sort by
in 1.7.02 the default sort order is in the configuration.
Config > Catalog > Catalog > Frontend >Product Listing Sort by
Does this work with Magento CE 1.7.0.2?
Should work with 1.7.0.2
I have installed frontend link manager extention from mage PSYCHO, and then soon after installed sort by newest extention from you. However, I now don’t see toplinks except my cart, and my account page has totally disappeared .
Can you get me the fix? It looks like these two extentions are crashing to each other. Please let me know. Thank you
Is it possible to remove completely from the website? I tried removing the files from the database, but there is still drop down menu on the admin panel for newest sort order. I still can not get the toplinks to work out.
You probably have a different problem. This extension doesn’t affect the top links at all. The only thing it does is change the “created_at” product attribute in the database. No files are changed or added at all. So, the conflict is not with this extension but something else in your site.
If you want to remove the “Newest” in the drop-down, you need to go into PHPMyAdmin or another database tool and find the created_at attribute_id in the eav_attributes table, then look for it in the catalog_eav_attribute table and update the used_for_sort_by column to 0.
However, you will most likely find that this will not fix your problem.
I found the created_at attribute,
but I can not find the used_for_sort_by column in the catalog_eav_attributes table,
or the used_for sort_by column. Where else could it be?
Thanks
Look again.
It’s there in catalog_eav_attribute if you don’t have a very old Magento version ( < CE 1.4)
Thank you for all your help. I got it working on most of the pages.
However it seems to not affect the search result page.
Is sorting search result page even possible?
I tried many different solutions from other sites including editing the line just before tag in form.mini.phtml, but no help.
I thought you could be the one to help with this.
Thank you
Oh I forgot to mention toolbar is showing but I disabled the sorter for customers not to adjust.
When the toolbar and sorter is enabled the newest function works, but when the sorter is disabled from toolbar,
the default setting is something different, and I don’t know where to change the setting for result page.
Great job Robert. It is working well in 1.7.0.2
How can you do it? Please help me :T_T:
Hello, please could anybody help me how to completely remove this module? I have serious problems after installing it. I have still errors with max_user_connections to database. I really need to “uninstall” it. Installation was by copying it to root/app folder.
Thank you.
Same problem as Lman. Does anybody knows how to uninstall it?
Same problem as Lman
If you want to uninstall this remove all the files you copied and follow my comment here: http://magebase.com/magento-tutorials/magento-sort-by-newest-products-made-easy/comment-page-1/#comment-2382
Keep in mind that this extension does not run any code once it installs itself. It only changes the created_at attribute in the database so it appears as an option for sorting.
Thank you Robert!
You were right! I hadn’t read that comment…
Once again, you made my day
Thanks for sharing, there’s never enough guys like you
It’s not working for me…
Anything I should know?
Only to clarify:
. What I did to install it was extract the folder to app and then close my browser and open it again. But I don’t have Sort by -> Date on my System -> Configuration -> Catalog / Product listin sort by… Any idea on what’s wrong?
Please, I really need this >_<
You should extract the files into your site docroot, not docroot/app/.
Works Great in magento 1.5 for me !!!
Tks Robert, it worked for me. Saved me hours!!! cheers mate.
I cannot do it. Can you tell me how to do it? Thanks…
Excuse me, could u tell me how to install and the local to install for the extension file that I just downloaded from here? Thanks…