Categories: How ToMagento 2

How to add Custom Sorting Field in the Category of Magento 2

Having a number of different products in your Store, it becomes a cumbersome task to find particular or similar products and that’s how the concept of categorized similar products plays a vital role in your store. Considering user convenience, Magento 2 allows you to create an unlimited number of categories from the backend along with the option of sorting products in the front that allows your customers to quickly find a particular product without scrolling thousands of products. But before you add products to the specific category first you need to create a product category from the backend and then assign products to its respective category.

Once you are done with categorizing the products, by default your customer can sort products based on position, name or price depending on selection. But many times it happens that if you are willing to serve personalized business experience depending on your business type you may need to add your own category sorting field in the frontend so your customer can quickly find the product that they are looking for. So, here we are back with another blog tutorial that in which we have added “Latest Products” sort order, but using this code you can add your own category sorting order in the Magento 2 frontend.
Firstly, we need to create “di.xml” file inside your extension etc folder and paste below code inside the file.
app\code\vendor\extension\etc\di.xml

<pre class="lang:default decode:true">
<?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="Vendor\Extension\Plugin\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>
</pre>

After that we need to create one more file “Config.php” inside your plugin folder using below given code.
app\code\Vendor\Extension\Plugin\Config.php

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\Plugin;
 
class Config
{
 public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
 {
     $optionsnew = ['latest' => __('Latest Products')];
     $options = array_merge($options, $optionsnew);
     return $options;
 }
}
</pre>

Lastly, we need to create one more file “Toolbar.php” file at this path using following code.

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

<pre class="lang:default decode:true">
<?php
namespace Vendor\Extension\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); // you can add filter as per your requirement.
     }
     return $proceed($collection);
 }
}
</pre>

That’s it! Now whenever your customer navigate to category page, he or she can find one more sorting option to sort product in the frontend.

If you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends.

And, Let us know if you are facing an issue while implementing this code.

Happy Sorting!

Click to rate this post!
[Total: 20 Average: 4]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate ?️ Certified Magento Developer?‍?. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.?

View Comments

Recent Posts

How to Add a Custom Field to the Cart Price Rules Form in Magento 2?

Hello Magento Friends, In this blog, we will learn How to Add a Custom Field…

7 hours ago

How to Add Tooltip in Checkout Shipping Field in Magento 2?

Hello Magento Friends, In today’s blog, I will explain How to Add Tooltip in Checkout…

3 days ago

How to Integrate and Use MongoDB with Laravel?

MongoDB is a popular NoSQL database that offers flexibility and scalability when handling modern web…

4 days ago

NodeJS | Callback Function

In NodeJS, callbacks empower developers to execute asynchronous operations like reading files, handling requests, and…

5 days ago

How to Show SKU in Order Summary in Magento 2?

Hello Magento Friends, In today’s blog, we will learn How to Show SKU in Order…

1 week ago

Best Colors to Use for CTA Buttons

The "Buy Now" and "Add to Cart" buttons serve as the primary call-to-action (CTA) elements…

1 week ago