How to Add Custom Sorting in Category of Magento 2

Most of the time our client wants to add custom sorting options on their website and most of the developers are looking for a solution for that. So, we have written this article for those developers who are looking for a solution to add custom filters on the category page.

In this article, I will give you simple steps to add custom sorting in the category of Magento. Please follow the below simple steps to add custom sorting in Magento 2.

Before we start I assume, you have already a created custom module. If you don't have it or don't know how to create it then check out our other article How To Create a Magento 2 Module.

Let's get started!!

1. Create a di.xml file in a module at the below path.

Copy
app\code\Devhooks\HelloWorld\etc\di.xml

Paste below code inside the file.

Copy
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
	<type name="Magento\Catalog\Model\Config">
		<plugin name="catalog_config_plugin" type="Devhooks\HelloWorld\Plugin\Config"/>
	</type>
	<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
		<plugin name="catalog_productlist_toolbar_plugin"type="Devhooks\HelloWorld\Plugin\Product\ProductList\Toolbar" />
	</type>
</config>

In above, Devhooks is a Namespace name and HelloWorld is a Module name. So, please don't forget to change it with your Namespace and Module Name.

2. Create a Config.php file in a module at the below path.

Copy
app\code\Devhooks\HelloWorld\Plugin\Config.php

Paste below code inside the file.

Copy
<?php
namespace Devhooks\HelloWorld\Plugin;

class Config
{
    public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
    {
        $optionsnew = ['latest' => __('Latest Products')];
        $options = array_merge($options, $optionsnew);
        return $options;
    }
}

3. Create a Toolbar.php file in a module at the below path.

Copy
app\code\Devhooks\HelloWorld\Plugin\Product\ProductList\Toolbar.php

Paste below code inside the file.

Copy
<?php
namespace Devhooks\HelloWorld\Plugin\Product\ProductList;

class Toolbar
{
    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        $currentOrder = $subject->getCurrentOrder();
        if ($currentOrder == "latest") {
            $dir = $subject->getCurrentDirection();
            $collection->getSelect()->order('created_at' . $dir);
        }
        return $proceed($collection);
    }
}

4. Open Command line in folder root of magento and run the below command.

Copy
php bin/magento cache:clean
//or
php bin/magento cache:flush

That’s it! Now visit your site and navigate to category page, and check new sorting option.

I hope this Magento article helped you to find what you were looking for.

Bookmark it for your future reference. Do comment below if you have any other handy commands which are not included in the list.

P.S. Do share this note with your team.




Search
Recent Posts
    Tags
    Newsletter
    SocialFollow on Twitter
    Visit now!