How to Sort Magento 2 Category Products by Stock Quantity

How to Sort Magento 2 Category Products by Stock Quantity

A visitor lands to your category page, scroll downs, see out of stock products first, gets disappointed and leaves the site. He left the store with a bad impression by seeing more out of stock products first and thus he even didn’t bother to scroll but leave.

For Magento 2 store owners, it is important to design the frontend of the store keeping the product stock in mind. The products having more quantity must be featured or displayed in the first fold rather than out of stock products. If a customer orders an out of stock product, his order processing takes time and hence it may deteriorate the customer experience and creates a first bad impression.

in Magento 2, if the user applies the sort options on product category such as products according to price, position or ratings, the default Magento 2 will display the products as per the filter applied. As users can’t apply two sorting options at a time, they will get results mixed with in-stock and out of stock products.

If you, as a Magento 2 store owner, want to combine the filter results of the users with stock quantity, you need to customize the functionality as default Magento 2 does not facilitate it. you have to custom code to sort Magento 2 category products by stock quantity.

I have given a stepwise solution to apply two sorting options at a time, one which is selected by the user and the other is the stock quantity based sorting auto applied programmatically.

 

Steps to Sort Magento 2 Category Products by Stock Quantity:

Step 1: Create registration.php at path app/code/Meetanshi/StockQuantity/registration.php

Step 2: Create module.xml  at path app/code/Meetanshi/StockQuantity/etc/module.xml

Step 3  Create events.xml at path app/code/Meetanshi/StockQuantity/etc/events.xml

Step 4: Create StockLast.php at path app/code/Meetanshi/StockQuantity/Observer/StockLast.php

The above method enables to apply two sorting options at a time on the category page to display products list on the basis of stock availability.

Hopefully, the post may have helped you sort Magento 2 category products by stock quantity, keeping the user experience at first place!

I’d be pleased to solve any doubts regarding the topic. Please mention them in the comments section below.

Rate the post with 5 stars if you liked it.
Happy Coding!

Thank You 🙂

Get Weekly Updates

Never miss Magento tips, tricks, tutorials, and news.

Thank you for subscribing.

Something went wrong.

4.8
(based on 28 Reviews)

Sanjay is a co-founder at Meetanshi. He is a Certified Magento Developer who loves creating Magento E-commerce solutions. Owing to his contributions in Magento Forums and posting solutions, he is among the top 50 contributors of the Magento community in 2019. When he is not engrossed with anything related to Magento, he loves to play cricket.

20 comments On How to Sort Magento 2 Category Products by Stock Quantity

  • It´s not working for Magento 2.4. Any advice?
    I created, no errors… nothing happened

    • Hello Frank,
      The above solution is not compatible with Magento 2.4
      We will post the solution in future.
      Do subscribe to get notified for the same.
      Thank you.

  • Hi ,

    When i run this command setup:upgrade then this error is showing. How to i can fix this issue. Please check attached screenshot.

    https://imgur.com/55idkO7

    Thanks

  • I run a command di:compile getting an Error

    Parse error: syntax error, unexpected ‘ ‘ (T_STRING), expecting function (T_FUNCTION) in /home/tonevint/public_html/app/code/Meetanshi/StockQuantity/Observer/StockLast.php on line 10
    Screenshot : https://prnt.sc/vzmq5o

    • Hello Naeem,

      In step 4: Create StockLast.php at path app/code/Meetanshi/StockQuantity/Observer/StockLast.php, you may have made a mistake while copying the file. Please do adjust the space. For me, it’s working properly with no error.

      Thank you.

  • Hi, this solution is not sorting products by stock quantity. It is checking only “in_stock”. I need to show items with greater quantity at first and the items with smaller quantity after that. Here is this happening but this code is in M1. Can you make one like this
    https://magento.stackexchange.com/a/44639/67038
    Thanks

    • Hello Syed,
      With this solution, the out of stock product will be displayed at last
      To do it quantity wise, you need to set conditions accordingly.
      Thanks.

  • Thanks for you solution. This is working in Magento 2.3.2-p2 with Search Engine MySql. But, If we configure the Elasticsearch 6+ that code is not working. Please check the code with Magento 2.3.2-p2 with Elasticsearch6+ .

  • hello
    is it possible to do the following action with your module.
    Frit red products with a stock higher than 2000?

  • sudo php bin/magento setup:upgrade

    PHP Parse error: syntax error, unexpected ‘ ‘ (T_STRING), expecting ‘,’ or ‘)’ in /var/www/html/magento2/app/code/Meetanshi/StockQuantity/registration.php on line 3

    • fixed above error goy
      t new on di:compile please advice
      /var/www/html/magento2$ sudo php bin/magento setup:di:compile
      Compilation was started.

      Repositories code generation… 1/7 [====>———————–] 14% < 1 sec 60.0 MiBPHP Parse error: syntax error, unexpected ' ' (T_STRING), expecting function (T_FUNCTION) or const (T_CONST) in /var/www/html/magento2/app/code/Meetanshi/StockQuantity/Observer/StockLast.php on line 10

  • Worked like a charm for magento 2.3
    Many thanks

  • Not working with 2.3.1, can you please update it

    • The solution works fine with Magento 2.3.1
      Please check if you have used the proper website id. We have used the static one and maybe that is causing the issue for you.

  • Dear sir Thank you

Leave a reply:

Your email address will not be published.