How to Add Magento 2 Sort by Price for Low to High & High to Low Options
The store design and its navigation must be in such a way that makes it easier for the shopper to find the exact required product and make the shopping process comfortable and enjoyable. Navigation can be made easier and hence improve the shopping experience by offering custom sorting options.
The default Magento 2 offers sorting by position, product name, and price, as shown below:
However, for a full-fledged Magento 2 store, these options are not enough.
Sometimes, people tend to have some specific budget for product purchase. A price-sensitive customer may save some clicks by starting with the cheapest products. On the other hand, customers who have a high standard for quality may quickly find their most desired products by sampling from high prices to low prices. To provide such feature in Magento 2 and serve both the type of price-sensitive customers, you can add Magento 2 sort by price for low to high & high to low options as shown here:
For doing so, follow the method given here.
Method to Add Magento 2 Sort by Price for Low to High & High to Low Options:
- Create registration.php file in app\code\[Vendor]\[Namespace]\
12345<?php\Magento\Framework\Component\ComponentRegistrar::register(\Magento\Framework\Component\ComponentRegistrar::MODULE,'[Vendor]_[Namespace]',__DIR__); - Create module.xml file in app\code\[Vendor]\[Namespace]\etc
12345<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd"><module name="[Vendor]_[Namespace]" setup_version="1.0.0"/></config> - Create di.xml file in app\code\[Vendor]\[Namespace]\etc
12345678910<?xml version="1.0"?><config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"><type name="Magento\Catalog\Block\Product\ProductList\Toolbar"><plugin name="custom_custom_block_toolbar" type="[Vendor]\[Namespace]\Plugin\Catalog\Block\Toolbar"/></type><type name="Magento\Catalog\Model\Config"><plugin name="custom_catalog_model_config" type="[Vendor]\[Namespace]\Plugin\Catalog\Model\Config"/></type></config> - Create Toolbar.php file in app\code\[Vendor]\[Namespace]\Plugin\Catalog\Block
12345678910111213141516171819202122232425262728<?phpnamespace [Vendor]\[namespace]\Plugin\Catalog\Block;class Toolbar{public function aroundSetCollection(\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,\Closure $proceed, $collection){$currentOrder = $subject->getCurrentOrder();$result = $proceed($collection);if($currentOrder){if($currentOrder == 'high_to_low'){$subject->getCollection()->setOrder('price', 'desc');}elseif ($currentOrder == 'low_to_high'){$subject->getCollection()->setOrder('price', 'asc');}}else{$subject->getCollection()->getSelect()->reset('order');$subject->getCollection()->setOrder('price', 'asc');}return $result;}} - Create Config.php file in app\code\[Vendor]\[Namespace]\Plugin\Catalog\Model
12345678910111213<?phpnamespace [Vendor]\[namespace]\Plugin\Catalog\Model;class Config{public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options){$options['low_to_high'] = __('Price - Low To High');$options['high_to_low'] = __('Price - High To Low');return $options;}}
It is safe to say that sorting and navigation are important elements of the Magento 2 store’s design. The above method is just right to improve the sorting and offer options to sort the products by price for low to high & high to low.
Please use the Comments section below if you have any doubts on the topic, Also, do share how this technique was helpful to you in terms of improving the customer experience and store design!
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
34 Comments
Hi, this code doesn’t work for magento 2.4.3.
Is there any update
The above solution is not compatible with Magento 2.4.3.
We will post the solution in future.
Do subscribe to get notified for the same.
Is there any updated code which works for 2.4.3?
Hello Gopal,
No, we will post the solution for 2.4.3.
Thank You
Hello Sanjay Jethva
i am trying to above extesion but error come Exception #0 (Elasticsearch\Common\Exceptions\BadRequest400Exception): {“error”:{“root_cause”:[{“type”:”query_shard_exception”,”reason”:”No mapping found for [low_to_high] in order to sort on”,”index_uuid”:”AAKHmvSmReyf0c1SFhQfZA”,”index”:”kalaniketan_product_1_v4″}],”type”:”search_phase_execution_exception”,”reason”:”all shards
Hello Ashok,
Which Magento version you’re using as the above solution will only work for Magento 2.2.x and 2.3.x
Thank You
Hello,
I use Magento 2.4.3 and when apply the sorting returns an empty page.
Do you have any idea why happening it?
Please let me know
Thanks
Hello Udhai,
The above solution works with Magento 2.2.x and Magento 2.3.x version.
Thank You
Hello Sanjay Jethva, The above code is not working in the latest versions of Magento 2.4.*
Sorting options are showing fine but getting blank results after selecting any option (Price – Low To High, Price – High To Low).
Hello Vishal,
The above solution works with Magento 2.2.x and Magento 2.3.x version.
Thank You
Not Working
Hello Chirag,
In which version you’re trying to execute this code?
The above code is working from our end
Thank You
Hello,
I use Magento 2.4.0 and when apply the sorting returns an empty page. I debug that and I observe that the collection into aroundSetCollection function is empty. Do you have any idea why happening it? Thanks
Hello Nick,
Place the log in the plugin and check if you’re getting the collection data in the first line or is it empty?
If it is empty, it may be possible that the collection is set by some other extension.
I hope it helps.
Hi,
I am getting following error:
Uncaught ArgumentCountError: Too few arguments to function Namespace\\Test\\Plugin\\Catalog\\Model\\Config::beforeGetAttributeUsedForSortByArray(), 1 passed in /var/www/html/demo/vendor/magento/framework/Interception/Interceptor.php on line 121 and at least 2 expected in /var/www/html/demo/app/code/Namespace/Test/Plugin/Catalog/Model/Config.php:5\nStack trace:\n#0 /var/www/html/demo/vendor/magento/framework/Interception/Interceptor.php(121):
Hello Vijay,
In the file [Vendor]\[namespace]\Plugin\Catalog\Model\Config.php, replace funciton
public function beforeGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options, $requestInfo = null)
With this code
public function beforeGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options, $requestInfo = null)
Thank you
I followed your instructions. But when i’m opening my category page there is no change on my sorting dropdown.
I’m using Magento ver. 2.3.0 & Porto theme
Hello,
The above solution works for default Magento but you will have to check for your theme.
Also, put the log and check if the Magento\Catalog\Block\Product\ProductList\Toolbar file is not overridden.
Check if the file is called properly.
Thank you.
I have the same problem in M2.3. Interceptor.php has 2 arguments, but sends only 1, i think that the options array Magento sends is empty, so i don’t think thats the correct way in M2.3
Hey,
Can you please check Magento core file which is used by plugin?
Thanks.
Price – Low To High
Price – High To Low
Can we show this options on top of position sorting option like this :
Sort By:
Price – Low To High
Price – High To Low
Position
Product Name
Price
How to show help me in this regards
Hi Moyeez,
Follow this: https://magento.stackexchange.com/a/201440/24801
It has a similar solution to your requirements. Make changes accordingly to our code and you will get the desired result.
Hello i tried in magento 2.1.7 and showing error like this
be 1 exception(s):
Exception #0 (Exception): Warning: Missing argument 2 for Vendor\Extension\Plugin\Catalog\Model\Config::beforeGetAttributeUsedForSortByArray(), called in /home/floristi/public_html/vendor/magento/framework/Interception/Interceptor.php on line 123 and defined in /home/floristi/public_html/app/code/Vendor/Extension/Plugin/Catalog/Model/Config.php on line 6
Hello Chirag,
Please refer Magento\Framework\Interception\Interceptor according to Magento 2.1.7
Thanks
Uncaught Erorr in Config.php file
Hello,
The issue seems to be in the extension that you created.
The above solution works fine.
Thank you 🙂
Hi,
The sorting is not working with configurable product with different child prices. Which means i have selected size in layered navigation and selected sort by price low to highoption.
Hey Adarsh,
The above solution is for visible product only and won’t work with the child products.
Thanks.
Uncaught ArgumentCountError: Too few arguments to function
It seems as if something has gone wrong while creating the module. Please make sure the decompile is working well.
Hi, I tried your above code for sorting. “Low to high” sort order not displaying correct results. It’s showing first product with 22.99 price second is 19.99. Incorrect results. “High to low” sort order works fine. Any help?
Please debug and check Toolbar.php
It will return products collection.
You can use debug log for
$subject->getCollection()->getSelect()
or$subject->getCollection()->getSelectSql()
and check queryIt says There are no commands defined in the “setup” namespace.
It seems that some things are missing while creating the module.
e.g. You will need to replace [Vendor] with Your Vendor name like Meetanshi and [Namespace] with Your Namespace like CustomSort