What are Webhooks in Magento 2
Discover webhooks in Magento 2: a powerful tool to reduce manual workload for business owners. Learn how they work, their key features, and easy configuration tips.
Vinh Jacker | 11-11-2024
Fed up with the limited default sorting options in Magento 2, including just three basic options: Position, Product Name, and Price? This article offers a detailed guide on adding Magento 2 sort by newest function – a popular shopping choice nowadays. People tend to choose the latest versions or items in all fields, from high-fashion clothing to cutting-edge electronics. Therefore, having the newest product sorting function in Magento 2 would benefit online businesses.
Let’s start!
In order to sort products by newest in Magento 2, please follow these steps below one by one.
At this very first step to add Magento 2 sort by most recent, you need to make a di.xml file in Vendor/Module/etc as follow:
<?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>
Next, the next must-do step is generating a Config.php file in Vendor/Modulename/Plugin/Model.
<?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;
}
}
In the Vendor/ModuleName/Plugin/Product/ProductList, create a Toolbar.php file with the following instructions.
<?php
namespace Vendor/ModuleName/Plugin/Product/ProductList;
use Magento\Store\Model\StoreManagerInterface;
class Toolbar
{
protected $_storeManager;
public function __construct(
StoreManagerInterface $storeManager
)
{
$this->_storeManager = $storeManager;
}
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);
}
}
Your Magento 2 store’s product sorting function is ready to go live at this point. It’s important to quickly check your changes later, though. Navigate to your store frontend to double-check the result. The newest products should be shown first by now.
This ensures that all of your notice bars work properly and without any problems. Additionally, you can move right away if something goes wrong.
Delivering exceptional customer experiences is an ongoing process, and providing out-of-the-box functionalities is essential for customer convenience. Besides the Magento 2 sort by newest function, there are plenty of other trending options online shops need to catch if they don’t want their store to be out-of-date. To enhance user satisfaction, utilizing an advanced sorting extension is highly recommended.
The Improved Product Sorting for Magento 2 extension is an excellent choice, providing valuable support to online shoppers in quickly finding their preferred products through efficient sorting. This extension significantly enhances customer experience during shopping, leading to increased sales for online stores.
Add 12 product sorting options which are flexibly customizable
Check it out!Additionally, this module also includes “New Arrivals” or Newest product sorting function in Magento 2.
The Magento 2 Improved Sorting extension enables seamless and comprehensive product sorting across 12 criteria, empowering customers to filter products effortlessly. Sorting options include Discount, Best Seller, Most Viewed, Rating, Review Count, New Arrivals, Stock Quantity, Wish List, Product Name, Price, Position, and Relevance.
Customers can effortlessly refine product groups on both the Search Page and Category Page, swiftly discovering their desired items for easy cart selection. This time-saving feature significantly enhances customer experience, leading to a smoother shopping journey.
With Mageplaza Better Sorting, admins can effortlessly customize labels for each criterion, enabling store owners to create appealing labels for sorted product groups such as Hot Deal, Crazy Sales, 24h off, and more. Customers are quickly drawn to these attractive and motivational labels, leading to increased clicks and further exploration. Additionally, these labels enhance the Sort By section on both the Search Page and Category Page, making it more user-friendly and dynamic.
Another intriguing feature of Magento 2 Improved Sorting by Mageplaza is the ability to set positions for each sorting label, with effortless configuration from the admin backend. This feature is handy for store owners aiming to prioritize certain product groups during marketing campaigns. For instance, to swiftly draw customers’ attention to sale-off product groups, setting them to display at the top of the sorting section on the storefront is easily achievable.
Navigate to the Magento Admin, select Stores > Configuration > Mageplaza > Better Sorting.
There are 13 sections in the following order:
Let’s find out the detailed requirements in each section and how to configure them.
Choose Yes to activate the extension and start sorting products as you wish.
There are 3 options in this part. In detail:
Note: Do not show the out-of-stock items at the bottom of the page.
Based on the stock label: Access to Catalog > Products, change the Stock Status field into Out of stock for any items displayed at the bottom page, though they are still in inventory.
On the search page, the product is sorted by default. If the first sorting type is disabled, the second is applied. For instance, when Discount (the first sorting type) is not selected, the module will display products based on Bestseller (the second sorting type).
Basically, these fields require the same information. Let’s take the Discount section as an example.
Insert a name to sort products by Discount. Remember: this is a mandatory field.
Choose product sorting with Discount by Percent or Amount. The items with more discount will get the priority to come up before others.
Select where to show product sorting by Discount. You can choose between 2 options:
Choose the default sort direction with Discount:
Insert a number in the Sort Order field to decide the default display order on Sort by. Lower numbers mean high priority. This field is mandatory, so if left blank, the system will remind you with a message: Please enter a valid number in this field.
This requirement appears in Most Viewed and Bestseller. In this part, you should enter a number to indicate the duration count on the selected sorting type. In particular, if you want to display the most recently best-selling products within the last ten days, insert number 10 in the box. If you leave it blank, the system will automatically set a default duration for the entire time.
Again, the requirements are similar in these sections except that they don’t require the Base on amount/ percent section.
Important note!
With the New Arrivals or sort products by newest in Magento 2, items are only included when the admin sets the field Set Product as New From for them. The most recently in-stock products will be shown first, followed by those that came in stock earlier. For products that are not chosen in this sorting type or have not been set up, the time in the Set Product as New From field will not be considered for New Arrivals. To set New Arrivals, access Catalog > Product, choose to edit any product and go to Set Product as New From.
The last two sections are similar as they require the same information. The difference here is that you only need to fill in four parts: Enable, Label, Default Sort Direction, and Sort Order.
Important note
With Position, Sort By Position, when accessing Catalog > Categories, you can apply any category for Sort By Position, then click on the Products tab in Category to see their Positions.
We hope the three fundamental steps to add the Magento 2 sort by the newest function above will be helpful for your online business-building journey. If you want to upgrade your store to a new level with more advanced options, we highly recommend integrating the Magento 2 Sorting extension - the supportive module to easily and quickly find preferred products via various popular sorting out choices.
Add 12 product sorting options which are flexibly customizable
Check it out!Related post: