add magento 2 sort products by newest

How to Add Sort by Newest Product Option in Magento 2

by nkhuyen

Incorporating the “Sort by Newest Product” functionality in Magento 2 will be a great add-on to any eCommerce website. After all, it allows customers to easily see new arrivals in your collection, enabling you to introduce new stock, create excitement, and increase sales. Now, how to add Magento 2 sort products by newest option?

In this tutorial, BSS Commerce will examine why it is important to have this option and how to add a sort by newest product option in the Magento 2 store in a step-by-step guide.

Now, let’s get started!

Why Should Add Sort by Newest Product Option in Magento 2?

Exceptional customer experiences can be viewed as a journey without a destination. It is also imperative to provide new features that contribute to the enhancement of customer ease. Moving the products that fall under the customer’s unique requirements to the top of the page can greatly help customers who have no idea what they want.

The basic Magento 2 package contains three possible sorting of products on the category page:

  • Product
  • Position
  • Name

Nonetheless, these default sorting options are not enough to improve and keep user effectiveness. to the libraries, customers, and readers, further sorting options seem very needed. There is no doubt as well that customers possess a unique sense of curiosity and often like checking out the latest products.

In this guide, BSS Commerce recommends that there be a sort by newest product functionality in Magento 2. In the next part of this article, BSS Commerce presents 3 simple steps to add sort by newest product option in Magento 2 which all Magento store owners should pay attention to.

Add Magento 2 Sort Products by Newest Option in 3 Steps

Step 1: Create di.xml file in Vendor/Module/etc

<?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 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 this step, you need to add toolbar.php file in the Vendor/ModuleName/Plugin/Product/ProductList using this code

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

Following the above steps, you will observe the option to sort products by the newest arrivals on the Magento 2 category page, as illustrated below:

how to add sort by newest product option in magento 2 1

In addition, if you are using a newer version of Magento (2.4.4 and later), the aforementioned method may not function correctly. In these instances, please adhere to the following steps:

  • Step 1. In the eav_attribute table, locate the created at product attribute and modify the frontend_label column value to “Created At.”
  • Step 2. In the catalog_eav_attribute table, identify the created_at product attribute and change the used_for_sort_by column value to 1.
  • Step 3. Clear the Magento 2 cache.

>>> You may need to know: Magento 2 create product attribute programmatically

On the other hand, this can be accomplished by executing the following SQL query:

# 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 about steps to add Magento 2 sort products by newest option. Congratulations! You are successful, added.

Using Magento 2 Improved Product Sorting Extension

Here is how you can add magento 2 sort products by newest with this extension

To initiate the configuration process, please navigate to Store ⇒ Configuration ⇒ BSSCommerce ⇒ Improved Product Sorting ⇒ Select Enable to activate this module

Enable Magento 2 Improved Product Sorting

Go through the Newest option

  • Select Yes to enable sorting of products based on their creation date or the date of the last modification.
  • Specify the sorting order: Ascending or Descending.

Now you have Magento product listing sorted by newest

Additionally, the extension offers more than just chronological product sorting – it also allows sorting by attributes such as Most Reviewed, Top Rating, Recently Ordered, Best Selling, Most Viewed, Average Views Per Sale, and ON SALE. These options are designed to highlight the most marketable elements, giving customers a diverse and appealing product selection.

Highlight Features Magento 2 Improved Product Sorting Extension

Next, select Yes to enable the product sorting option you want and then configure the sorting order by ascending or descending

Configure magento 2 improved product sorting extension

In Arrange Sort: In the sorting options box on the frontend, a drop-down menu of sorting choices is available.

Magento 2 Improved Product Sorting Extension 4

To change the arrangement of these options, simply drag and drop them with ease.

arrange sort

Additional configurations can be performed as per the following guidelines:

Magento 2 Improved Product Sorting Extension 6

You can set up the Meta Robot Tag and Canonical Tag to enhance SEO.

*Take Note:

When utilizing this module, it is essential to verify that the following configurations are enabled:
– Recently Viewed/Compared Products: Yes

Navigate to Stores > Configuration > Catalog > Catalog > Recently Viewed/Compared Products.

Magento 2 Improved Product Sorting Extension 7

– Enable Report: Yes

Go to Stores > Configuration > General > Reports > General Options

Magento 2 Improved Product Sorting Extension 8

If the category page or the dashboard does not reflect any changes after loading, please proceed with the following steps: Navigate to Reports > Statistics > Refresh Statistics.

In the Actions menu, select Refresh Lifetime Statistics.

In the Mass Actions section, opt for Select All.

Finally, click on Submit.

Magento 2 Improved Product Sorting Extension 9

The Magento 2 Improved Product Sorting extension helps in the provision of eight more shopping catalog sorting attributes to the customers for the ease of Magento store owners. This feature has been incorporated specifically to address the needs of the customers and increase sales, more so of the promotional products.

Magento 2 Improved Product Sorting extension

Bottom Line

Finally, the guideline “how to add Magento 2 sort products by newest option?” is complete. By following the steps outlined in this Magento 2 basic tutorial, you have successfully added a “Sort by Newest Products” option to your Magento 2 store. In addition, remember to regularly update your product catalog to ensure that the newest products remain prominently displayed, enticing customers to return and make additional purchases.

Also, you can consider using our specialized Magento Development Services. These services are tailored specifically for Magento, providing reliable and secure hosting solutions. This allows you to focus on improving your business without concerns regarding the performance and security of your website.

BSS Commerce believes that these recommendations will be advantageous for you. Should you have any inquiries or require additional support, please do not hesitate to contact us.

Next Reading Suggestions

© 2019 BSS Commerce owned by THANH CONG INTER ., JSC. All Rights Reserved.
Business registration certificate no. 0106064469 issued by Hanoi Department of Planning and Investment on 19 December 2019.
Legal Representative: Mr. Nguyen Quang Trung.