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.
If you are looking for a solution for Magento 2.4 and later then explore our another article which is How to Add Custom Sorting in Category of Magento 2.4 and Later
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.
saveCopyzoom_out_mapapp\code\Devhooks\HelloWorld\etc\di.xml
Paste below code inside the file.
saveCopyzoom_out_map<?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.
saveCopyzoom_out_mapapp\code\Devhooks\HelloWorld\Plugin\Config.php
Paste below code inside the file.
saveCopyzoom_out_map<?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.
saveCopyzoom_out_mapapp\code\Devhooks\HelloWorld\Plugin\Product\ProductList\Toolbar.php
Paste below code inside the file.
saveCopyzoom_out_map<?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.
saveCopyzoom_out_mapphp 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.
AI-Powered Recommended Articles
How to Add Custom Sorting in Category of Magento 2.4 and Later
Explore the process of adding custom sorting in categories for Magento 2.4 and later versions.
Magento 2 - How to Add a Custom Filed in Order Grid
Learn how to add a custom column to the admin order grid in Magento 2 for enhanced order tracking.
Magento 2 - How to add Custom Fields in Custom Option Value
Learn how to add custom fields in custom option values for Magento 2 products to enhance product customization.
How to Create a Custom Console Command in Magento 2
Learn how to create custom console commands in Magento 2 to extend the command line interface for your specific needs.
How To Create a Magento 2 Module
Follow this guide to create a custom Magento 2 module from scratch for extended functionality.
Magento 2 - How to Add Custom Input Type in Custom Option
Learn how to add custom input types to custom options in Magento 2, offering more flexible product customization options.