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 Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

3 days ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

5 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

5 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

6 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

7 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

1 week ago