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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

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 this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (delete "?>" if you have it there). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.3.

If you think this code saved you time & money, feel free to join 14,000+ WooCommerce Weekly subscribers for blog post updates or 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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.

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

  1. 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

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

  3. 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!

  4. 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?

  5. 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

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

    1. On the single product page? Nope

  7. 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!

  8. 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.

  9. 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!

  10. 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!

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

  12. 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

  13. 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!

  14. 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!

  15. 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!

  16. 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!

  17. Snippet 1 works well ! thank you !!

    1. Great!

  18. 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

  19. 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
     * @donate $9     https://businessbloomer.com/bloomer-armada/
     */
    
    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

  20. 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!

  21. 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!

  22. 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!

  23. 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

  24. 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 🙂

  25. 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

  26. 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 🙂

  27. 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? Support? Leave your Comment Now!
_____

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. 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 BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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