WooCommerce: Display “In Stock” Products First @ Shop

We’ve already seen how to add a custom “Product Sorting” option to the “Default Sorting” dropdown in the WooCommerce Shop page.

The task I was presented with, however, was to display items based on a custom “meta key”. Now, if you have no idea what a “meta key” is, don’t worry too much. For example, “_stock_status” is one of these keys, and therefore you can sort products by that key as opposed to product name, date, price, etc.

So, let’s see how to show all out of stock products as last in the shop, category and loop pages. As usual, simply copy/paste this little plugin in your functions.php and you’ll get the wanted result 🙂

WooCommerce: sort products by stock (push out of stock products to the end of every WooCommerce loop)

PHP Snippet: Sort Products by Stock Status @ WooCommerce Shop

Note 1: this snippet only works when you use “Default Sorting” from the WordPress > Customize > WooCommerce > Product Catalog > Default product sorting section. It won’t override sorting by price / popularity / etc. when these are set as default.

Note 2: this snippet sorts products by “stock_status” in “ASC” (ascending) order. Possible values are “instock” “outofstock” “onbackorder” so it will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock

Note 3: all your products must be using the “Managing Stock” option in the product edit page > Product Data > Inventory. Otherwise I’m not sure what happens!

/**
 * @snippet       Sort Products By Stock Status - WooCommerce Shop
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */

add_filter( 'woocommerce_get_catalog_ordering_args', 'bbloomer_first_sort_by_stock_amount', 9999 );

function bbloomer_first_sort_by_stock_amount( $args ) {
	$args['orderby'] = 'meta_value';
	$args['meta_key'] = '_stock_status';
	return $args;
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce Visual Hook Guide: Archive / Shop / Cat Pages
    I’ve created a visual HTML hook guide for the WooCommerce Archive Page (which is the same page for the Shop, Category, Tag pages). This visual guide belongs to my “Visual Hook Guide Series“, that I’ve put together so that you can find WooCommerce hooks quickly and easily by seeing their actual locations (and you can […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]
  • WooCommerce: How to Remove the “Default Sorting” Dropdown
    If the WooCommerce product sorting functionality (“Default Sorting” dropdown) is a waste of space or you don’t need that select box at all, you may want to remove it. No matter if you prefer custom code or a simple plugin – hiding the product sorting dropdown is a piece of cake. Enjoy!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

65 thoughts on “WooCommerce: Display “In Stock” Products First @ Shop

  1. Hi,

    the code works perfectly, but not for the search results, I also put ( is_search() ) but it doesn’t work, can you help me please?

    Thank you

    1. Hello Domenico, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Hello, thank you for this! It works great. However, I was wondering if theres a way to have it work on Sort by Price and Sort by Popularity as well?

    1. Hello Jessica, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  3. Unfortunately, this snippet breaks the “products” shortcode for displaying new products — the table of new products is not updated (or updated incorrectly) if the snippet is included.

    1. I guess that’s possible, sorry about that

  4. Hi. Works fine, but with the exception that the order was descending. I had to add ‘order’ = ‘ASC’ for it to work as described.

      1. Hello,

        How to do that? What to add in the code below?
        “Hi. Works fine, but with the exception that the order was descending. I had to add ‘order’ = ‘ASC’ for it to work as described.”

        add_filter( 'woocommerce_get_catalog_ordering_args', 'bbloomer_first_sort_by_stock_amount', 9999 );
         
        function bbloomer_first_sort_by_stock_amount( $args ) {
           $args['orderby'] = 'meta_value';
           $args['meta_key'] = '_stock_status';
           return $args;
        }

        Thanks!
        Best regards

        1. $args['order'] = 'ASC';
  5. Hi,

    This works great, however my client wants all in stock items to show first, but they also want the menu order setting to be respected for in stock items, is that possible?

    1. Hi Bob, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  6. Hi! I’ve tried your code but it didn’t work for me on my theme. I worked on standard WP themes to. What could I have in my theme that prevents this from working?

    I also have my default ordering from minimum to maximum price and an exception for a couple of pages inserted via code in the functions.php file.

    1. Not sure, this require custom troubleshooting so I can’t help here. Did you try contacting the theme developers?

  7. Hi, Code working well.
    Please consider create similar code on your blog but for products with images first . 🙂

    1. Thanks – I’ll see what I can do

  8. Can i use this snippet for sorting product variations?

    1. On the single product page? Nope

  9. The Code worked well for us so thank you 🙂

    But we have two shoes that doesn’t follow, not sure how that can be?
    As well if we want to add it to sort it on type, how would we do that?

    Thanks in advance

    1. Hi Christian, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom troubleshooting work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  10. For Information Only

    Strangely this did’nt work for me in WC 4.9.2 but Works in WC 5.0.0

    Thanks Rodolfo

    1. Update, It didnt work because site had “Post Types Order” plugin installed, after I deactivated that the snippet worked perfectly.

  11. Hi, nice trick

    Is there a way for this to work on shortcodes? I notice that it only works in shop page.

    1. Hi Josè, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  12. Hi your code works but does not take into account external / affiliate products in the order. Is there a way to see these products first and then eventually the out of stock products?

    1. Hi Alonso, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  13. Works beautifully – Let me a buy you a beer!

  14. Works great! Thanks. Is it possible to give green Color to the text?

    Thanks in advance

    1. Thanks! You can achieve that with simple CSS

  15. hi, how can order products by stock _status and date??

    1. Hey Jerax, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  16. Hi,
    there is a possibility to add first only product by some user id ?
    Thanks

    1. Hi Marco, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  17. hi
    how to show just in stock product in related product section?

    1. Hi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  18. How to make this as by default order

    1. Hi Anil, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  19. Snippet 1 works well ! thank you !!

    1. Great!

  20. it doesn’t work for me.
    what should i do?

    1. To troubleshoot, disable all plugins but WooCommerce and also switch temporarily to “Twentyseventeen” theme (load the snippet there in functions.php) – does it work? If yes, you have a problem with your current theme or one of the plugins.

      Hope this helps!

      R

  21. Hello and thank you for this function.
    If someone need to sort by stock status + other criteria un can try this changed code:

    /**
     * @snippet       Sort Products By Stock Status - WooCommerce Shop
     * @how-to        Get CustomizeWoo.com FREE
     * @author        Rodolfo Melogli
     * @compatible    WooCommerce 6
     * @community     https://businessbloomer.com/club/
     */
    
    add_action( 'woocommerce_product_query', 'bbloomer_sort_by_stock_status_then_alpha', 9999 );
     
    function bbloomer_sort_by_stock_status_then_alpha( $query ) {
        if ( is_admin() ) return;
        $query->set( 'meta_key', '_stock_status' );
        $query->set( 'orderby', array( 'meta_value' => 'ASC', menu_order => 'ASC' ) );
    }
    

    Just change menu_order with your criteria…

    1. Nice!

      1. this code created http 500 error for me

        1. You’re right, I’ve fixed it now for you. Let me know

          1. How would you amend this code to display products on backorder first, then in- and out- of stock?

            1. Hello Greg, this will automatically display them alphabetically like this: 1.instock 2.onbackorder 3.outofstock so we need to find a different workaround. This is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  22. Hi,
    I’m sorry about my English.
    I used your code and it works correctly.
    But now I have to change the order.
    Is there a possibility to order products for the best sellers and put out of stock products at the bottom of the page?

    Thank you
    Mirco

    1. Hi Mirco, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  23. Good day.
    I do everything according to your instructions.
    But I can not. It is necessary that the goods be sorted by quantity in stock. First of all, cho would show the goods of which are more in stock.
    Help please solve this problem.
    WordPress 5.2
    WooCommerce 3.6.2

    1. Hi Roman, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  24. Great snippet. How about one which sorts by in stock variables on the single product page ?? So when you 1st land on the page the first available in stock item is ready to buy instead of if the default item is out of stock?

    1. Hello Wayne, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  25. Hi there. We like this code and want to use it, but we also want to enable it so that ‘stock’ sorts it first by stock, then by title.
    So the page loads in Title order, but with ‘out of stock’ at the end of the pagination.

    I don’t see a way to do this, as ‘title’ isn’t a meta_value, like price and SKU are.

    Can you help?

    1. Hey Simon, thanks so much for your comment! Yes, this is possible – unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  26. Hi Rodolfo,

    Thank you again for all your work and support to the community.

    This code works wells. However it messes up the order of certain other places. For example, all Woo sites has the /shop/?orderby=date function, which we use to show the new arrivals in our store. But after adding this code, the new arrival products via the above link I pointed out is all a mess. It’s not showing the latest product which was added to the store anymore.

    Would appreciate if your feedback in this matter 🙂

    Thanks again!

    1. Hey Randika, thanks for your comment! You’ll need to either run this snippet just on the pages you want, or otherwise customize the coding to make it work with the other sorting options 🙂

  27. Hello,
    Is this possible to make the code work on single product page in Related products section?

    I mean – now my Related products section show often out of stock products and I would like to get rid of them or shift them to the end of the list (if there are only a few related products).

    Regards,
    Domin

    1. Hello Domin, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  28. Kinda works, but then you lose the alphanumeric sorting, so the “in stock” items are all at top, but no longer listed alphabetically. This outcome is on current WP & Woo as of November 2018.

    1. Hey Matt, thanks for your comment! I guess you can add an “orderby” array element and make that happen – let me know 🙂

  29. Hi.
    How can I show products by two model:
    first: Stock Ascending
    second: publish date Descending
    Thanks

    1. Hello Ushina, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *