How to Add Custom Sorting Field in Magento 2

Hello Everyone,

In this blog, we will learn about how to Add Custom Sorting Field in Magento 2.

By default Magento 2 gives three options for product sorting on category page.

  1. Position
  2. Product Name
  3. Price

But sometimes these default sorting options are not enough. So we need to create new sorting field in Magento 2.

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add Custom Sorting Field in Magento 2.

STEPS FOR ADD CUSTOM SORTING FIELD IN MAGENTO 2

Step 1: Create di.xml file

app/code/Vendor/Extension/etc/di.xml

<?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="catalog_config_plugin" type="Vendor\Extension\Plugin\Model\Config"/>

    </type>

    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">

        <plugin name="catalog_productlist_toolbar_plugin" type="Vendor\Extension\Plugin\Product\ProductList\Toolbar"/>

    </type>

</config>

Step 2: Create Config.php file

app/code/Vendor/Extension/Plugin/Model/Config.php

<?php

namespace Vendor\Extension\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

app/code/Vendor/Extension/Plugin/Product/ProductList/Toolbar.php

<?php

namespace Vendor\Extension\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);

        }

    }

}

Step 4: Finally run the below commands

$ php bin/magento cache:clean

$ php bin/magento cache:flush

Step 5: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add more Sorting Field in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog?

Stay tuned with us on our site to get new updates of Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 5 / 5. Vote count: 1

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *