How to Add Sort by “Newest Product” option in Magento 2
It’s never ‘job done’ when it comes to providing exceptional customer experiences. It is necessary to offer out-of-the-box functionalities to customers for their convenience.
Bringing products with certain criteria to the top of the page can be particularly useful for customers who aren’t exactly sure what they want.
Default Magento 2 provides three options for product sorting on category page.
- Product
- Position
- Name
To increase and maintain user efficiency, default sorting options are not enough. We have to make a remarkable effort to present more sorting facilities for customers.
Mostly customers are curious by nature and always affectionate towards browsing the latest products. Today, I’ve come up with the solution to add sort by newest product option in Magento 2.
Steps to Add Sort by Newest Product Option in Magento 2
1 . Create di.xml file in Vendor/Module/etc
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Catalog\Model\Config"> <plugin name="Vendor_ModuleName::addCustomOptions" type="Vendor\Module\Plugin\Model\Config"/> </type> <type name="Magento\Catalog\Block\Product\ProductList\Toolbar"> <plugin name="catalog_productlist_toolbar_plugin" type="Vendor\Module\Plugin\Product\ProductList\Toolbar"/> </type> </config> |
2 . Create Config.php file in Vendor/Modulename/Plugin/Model
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
<?php namespace Vendor\Modulename\Plugin\Model; use Magento\Store\Model\StoreManagerInterface; class Config { protected $_storeManager; public function __construct( StoreManagerInterface $storeManager ) { $this->_storeManager = $storeManager; } public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options) { $customOption['newest_product'] = __('Newest Product'); $options = array_merge($customOption, $options); return $options; } } |
3 . Create Toolbar.php file in Vendor/ModuleName/Plugin/Product/ProductList
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
change Vendor/ModuleName/Plugin/Product/ProductList/Toolbar.php <?php namespace Vendor\ModuleName\Plugin\Product\ProductList; use Magento\Catalog\Block\Product\ProductList\Toolbar as Productdata; class Toolbar { public function aroundSetCollection(Productdata $subject, \Closure $proceed, $collection) { $currentOrder = $subject->getCurrentOrder(); if ($currentOrder) { if ($currentOrder == "newest_product") { $direction = $subject->getCurrentDirection(); $collection->getSelect()->order('created_at ' . $direction); } return $proceed($collection); } } } |
Following the above steps, you can see the sort by newest product option in Magento 2 category page as shown below:
If you are using a new version of Magento (2.4.4 onwards), the above method may not work properly. In such cases, follow these steps:
-
In
eav_attribute
table, find thecreated_at
product attribute and set itsfrontend_label
column value toCreated At
. -
In the
catalog_eav_attribute
table, find thecreated_at
product attribute, and set itsused_for_sort_by
column value to 1. - Clear Magento 2 cache.
You can also do this by using the following SQL Query:
1 2 3 4 5 6 7 8 |
# Get the attribute_id of 'created_at' select attribute_id from eav_attribute where attribute_code = 'created_at' and entity_type_id=4; # Set frontend_label update eav_attribute set frontend_label = 'Created At' where attribute_id=112; # Set used_for_sort_by update catalog_eav_attribute set used_for_sort_by = 1 where attribute_id=112; |
That’s all!
Do you have any doubt? Mention it in the comment section below. I’ll glad to help you.
Also, let me know if you found any positive impact on purchase of new products after implementing the solution.
Please do consider sharing this post with Magento Community via social media.
Thank You.
Related Post:
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
8 Comments
for magento 2.4
Is there a way to overwrite the module and not do it directly in vendor?
Hii, No don’t do it directly in vendor,
As in our blog ‘Vendor/Module’ means any third party module like ‘Meetanshi/Custom’
in which you can use this code or you can create new module and use this code.
Reference https://meetanshi.com/blog/create-module-in-magento-2/.
Hello,
It’s showing this error when I’m selecting Newest Product, “We can’t find products matching the selection.”
Hello Dev,
Are you getting products without applying this? As this logic can only set in “Order” of product collection.
Thank You
I have applied this but” We can’t find products matching the selection.” I faced this same issue after sort by the newest products here I have attached the screenshot(https://prnt.sc/-jZV–54X2Ji)
Hey Dev,
Once please reindex and clear the cache.
Hi,
I am getting this error, when i am running compile command in magento 2.4.0.
1/8 [===>————————] 12% 1 sec 87.0 MiBPHP Parse error: syntax error, unexpected ‘$currentOrder’ (T_VARIABLE) in /chroot/home/a3c83517/3bc9524d91.nxcli.net/html/app/code/Jco/Jcosorting/Plugin/Product/ProductList/Toolbar.php on line 19
Parse error: syntax error, unexpected ‘$currentOrder’ (T_VARIABLE) in /chroot/home/a3c83517/3bc9524d91.nxcli.net/html/app/code/Jco/Jcosorting/Plugin/Product/ProductList/Toolbar.php on line 19
Hello Wajahat,
It seems like you’ve not used semicolon in the variable
Thank You