Cookies setting

Cookies help us enhance your experience on our site by storing information about your preferences and interactions. You can customize your cookie settings by choosing which cookies to allow. Please note that disabling certain cookies might impact the functionality and features of our services, such as personalized content and suggestions. Cookie Policy

Cookie Policy
Essential cookies

These cookies are strictly necessary for the site to work and may not be disabled.

Information
Always enabled
Advertising cookies

Advertising cookies deliver ads relevant to your interests, limit ad frequency, and measure ad effectiveness.

Information
Analytics cookies

Analytics cookies collect information and report website usage statistics without personally identifying individual visitors to Google.

Information
mageplaza.com

How To Add Magento 2 Sort By Newest with 4 Basic Steps

Vinh Jacker | 11-11-2024

How To Add Magento 2 Sort By Newest with 4 Basic Steps

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!

Detailed instructions for adding Magento 2 Sort by Newest

In order to sort products by newest in Magento 2, please follow these steps below one by one.

Step 1: Create di.xml file

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>

Step 2: Create Config.php file

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;
    }
}

Step 3: Create Toolbar.php file

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);
   }
}


Step 4: Check your results on the storefront

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.

An advanced option for better sorting

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.

Benefits

  • Improve customer experience
  • Quick product filtering
  • Add sorting functionalities to the Search and Category Pages
  • Reduce bounce rate & boost conversion rate
  • Professionalize store sites


better sorting

Better Sorting for Magento 2

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.

Highlight features

1. Diverse Product sorting options

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.

2. Sorting on Search and Category Pages

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.

3. Label customization

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.

4. Sorting arrangement

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.


Magento 2 extensions

Magento 2 extensions

Allow you to achieve more with your online store

Check it out!


How to configure the Better Sorting extension

Navigate to the Magento Admin, select Stores > Configuration > Mageplaza > Better Sorting.

General Configuration

There are 13 sections in the following order:

  • General Configuration
  • Discount
  • Bestseller
  • Most viewed
  • Top rated
  • Reviews Count
  • New arrivals
  • Stock Quantity
  • Wish list
  • Product name
  • Price
  • Position
  • Relevance

Let’s find out the detailed requirements in each section and how to configure them.

1. General Configuration

General Configuration

1.1. Enable

Choose Yes to activate the extension and start sorting products as you wish.

1.2. Show Out of Stock at the End

Show Out of Stock at the End

There are 3 options in this part. In detail:

  • Base on Qty: Show the Out of Stock products at the bottom of the page based on the quantity available (<1). Then head to Catalog> Products to adjust Quantity. If the item available is less than 1, the system will display those products on the page bottom.

Base on Qty

  • 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.

Base on label

1.3. Default Sort by on Search Page

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).

2. Discount, Bestseller, Most Viewed, Top Rated, Reviews Count

Basically, these fields require the same information. Let’s take the Discount section as an example.

Discount

2.1. Enable

  • Select Yes to allow sorting items by Discount products.
  • Select No to turn off sorting items by Discount products.
  • The next enable product type will be applied instead.

2.2. Label

Insert a name to sort products by Discount. Remember: this is a mandatory field.

2.3. Base on

Choose product sorting with Discount by Percent or Amount. The items with more discount will get the priority to come up before others.

  • Percent: Show products based on percentage discount.
  • Amount: Show products based on a fixed discount amount.

2.4. Apply On

Select where to show product sorting by Discount. You can choose between 2 options:

  • Category Page
  • Search Page

2.5. Default Sort Direction

Choose the default sort direction with Discount:

  • Ascending: set discount products in the increasing order
  • Descending: set discount products in the decreasing order

2.6. Sort Order

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.

2.7. Time base (days)

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.

3. New Arrivals, Stock Quantity, Wish List, Product Name, and Price

New Arrivals

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.

Set Product as New From

4. Position and Relevance

Position and Relevance

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.

Conclusion

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.


better sorting

Better Sorting for Magento 2

Add 12 product sorting options which are flexibly customizable

Check it out!


Related post:

Better Sorting - A Smart Filter for online stores’ visitors

Magento 2 Sort by Popularity / Best Seller

Table of content
    Jacker

    With over a decade of experience crafting innovative tech solutions for ecommerce businesses built on Magento, Jacker is the mastermind behind our secure and well-functioned extensions. With his expertise in building user-friendly interfaces and robust back-end systems, Mageplaza was able to deliver exceptional Magento solutions and services for over 122K+ customers around the world.



    Related Post

    Website Support
    & Maintenance Services

    Make sure your store is not only in good shape but also thriving with a professional team yet at an affordable price.

    Get Started
    mageplaza services