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!