Hide Out of Stock Products from Your WooCommerce Catalog Pages

You may want to hide all out of stock products (including variations added by our Show Single Variations plugin) from your WooCommerce catalog. In this post I’ll show you a couple of ways to do it; using your own code or using a global WooCommerce Setting. Take note of your version of WooCommerce, as the code based option is different between versions.

Hide Out of Stock products for WooCommerce 3.0.0 and above

WooCommerce changed the way visibility settings are applied to products in version 3.0.0 and above. As such, the code is slightly different to hide them from the catalog. If you’re using a version of WooCommerce greater than 3.0.0, then simply add the following snippet:

Product visibility is now handled in the form of a taxonomy, rather than post meta. So the above snippet will show all products as long as they are not set to outofstock. This includes product variations.

Psst! Want to show product variations on your shop pages?

Try the 14-day free trial of WooCommerce Show Single Variations and display product variations in your WooCommerce shop pages as though they were simple products.

Get Free Trial

Hide Out of Stock products for older WooCommerce versions

For older versions of WooCommerce (versions before 3.0.0), just add the following snippet:

This just adds a new meta query to the main query that says only show products that don’t have an out of stock status. Sorted!

Hide Out of Stock products using the WooCommerce global setting

If you want less control, but a much more simple solution, you can just toggle the WooCommerce setting which will do all of this for you. Navigate to WooCommerce > Settings > Products > Inventory, and check Hide out of stock items from the catalog. Doing this will hide out of stock items everywhere on your site; in recently viewed widgets, shortcodes, search, catalog pages. Everywhere!

37 Comments

  1. mmice says:

    Hi! I have Woocommerce 3.0.6, and the snippet for old versions works awesome, though the one for new versions doesn’t

    • James Kemp says:

      Hey,

      Did you try toggling the option directly in WooCommerce > Settings?

      • mmice says:

        Of course! The first thing i tried cause weird shit. As i have more than half products “out of stock”, i get empty 1st page. And all counters show 5K products present though only 2,5K are in stock… And so do all the widgets.

        • Mooiesite.nl says:

          I have the exact same problem. Pages with 8, 5 or 9 products showing, saying the page should be showing items 1-12 of a total of 45… It’s easy to see what’s wrong: sold out items don’t appear on the page, but they are definitely counted everywhere.

          Changing the theme, disabling all plugins, clearing transients, it doesn’t seem to make any difference.

  2. Tim Griffin says:

    What about the related product display? I used to be able to do this pre-3.0 in the related.php template. What I want is to display out of stock product everywhere EXCEPT in the related products list on single product. Suggestions?

  3. JermaineHercules says:

    Hello, I’ve been looking for something like this but instead you check if the product has a variation which is out of stock then hide the parent. So far I have this https://gist.github.com/ano

  4. A.Samad Ghanchi says:

    I want all out of stock products shows in last and all feature product shows first … is this possible ?

  5. Christophe Hollebeke says:

    Both snippets doesn’t work for me on a new clean webshop with Woocommerce 3.1.1 🙁

    • James Kemp says:

      Hey, I’ll double check that, but did you also try the setting in WooCommerce (see the last heading in the blog post)?

      • Shideh Ghajar says:

        I have same problem as Christophe, and I have the setting setup and still it is showing products that are out of stock.

        • Christophe Hollebeke says:

          Hi,
          I double check the Woocommerce settings page. The ‘Hide out of stock items…’ was unchecked
          I found a solution, and post it on Github:
          https://github.com/ctuxboy/
          It displays a custom text: ‘Out of Stock’, if a variation is ‘out of stock’
          Hope this can help others.

          • James Kemp says:

            Just to be clear “Hide out of stock items…” should be checked if you want to hide out of stock items! Thanks for sharing the code.

          • Christophe Hollebeke says:

            Hi James, yes i’m trying check/uncheck the setting in Woocommerce, but don’t work. The important thing is that it’s work now 😉
            You can see it here on a test webshop: https://www.youtube.com/wat

    • James Kemp says:

      I’ve just tested this snippet again and it seems to work as expected. I wonder if something else is at play. What theme are you using? Have you tested with Storefront?

  6. Drew Malaki says:

    Do you have solution on woocommerce 3.1.1, when the setting to hide out stock is checked yes its working but the catalog or archive page pagination is broken but when I make the out of stock display the pagination is correct. example in a page it should display 10 items but some page is 6 out 10 items display only. Suggestions?

    • James Kemp says:

      Hey,

      Sorry for the delay in getting back to you! I’ve just tested the “toggle” option with Woo 3.2.5 and the counts all appear to be correct now. Have you noticed the same?

  7. Rub Fox says:

    I need some help running the code on specific page, I want to have one page displaying all the products including the ones out of stock and other page displaying only the product on stock, the first is easy using the Woocommerce settings, but the make the second one to show only products on stock?

  8. Amal Graafstra says:

    What about hiding products that have no regular price set (blank price, not $0/free)

  9. Alexander Skibinskiy says:

    Hey, James! I have a store: http://www.leador.ro/butic, we sell unique products (1 item only) I want to have an option that will allow manually to hide from catalog OUT of stock products but in the same time make out of stock products visible for SEO. Do you have a plugin for that?

  10. V. Ionescu says:

    Hi,
    Need to hide out-of-stock products, but not those that have been sold out today, last week, or last 30 days. Meaning if it is sold out right now, still need to show it.
    Any clue?

  11. Heng See Keat says:

    Hi James, can i know do u have any solution to hide “out of stock” products in Homepage/Main Page only?

    • James Kemp says:

      Yep, just change this line:

      if ( ! $q->is_main_query() || is_admin() ) {
      return;
      }

      to

      if ( ! is_front_page() || ! $q->is_main_query() || is_admin() ) {
      return;
      }

      • Heng See Keat says:

        Hi James, thanks for you fast reply, just try replace this code to test, but it’s not working, can i know why?
        If i just follow your sample code, it’s working on my catalog page, after i change the code you provide just now it’s not working on my homepage.
        Please advice, thank you.

  12. Omanhene Cocoa Bean Company says:

    Running Woo 3.3.1 (latest version of Woo and WP) and this php snippet does not seem to work. Has it been updated recently? I am not seeing it throw any php errors. How does one go about debugging something like this if there are no php errors in the log or onscreen? If someone else could weigh in and just confirm that it does still work fine for them on 3.3.1, it would help me to know how to proceed.

  13. Omanhene Cocoa Bean Company says:

    The top version does not seem to work for the latest version of WooCommerce. Since this thread is quite old, before I kill myself trying to debug this (which I am ill equipped to do), can anyone weigh in on whether this snippet is still working for them when using WP ver 4.9.4 and WooCommerce 3.3.1? Or is there some other solution that has evolved in the last 6 months?

    And/or any tips on how to debug something if it is not throwing any errors? Thanks in advance for listening in on this old thread!

    • James Kemp says:

      Hey, I assume you’ve tried simply checking the setting in WooCommerce? (Last paragraph in the blog)

      • Omanhene Cocoa Bean Company says:

        Yes. (And to be clear, this is for individual variations on a product that is out of stock.) The behavior I see is If you check the Woo box for OOS visibility, your OOS variations still show a color swatch, or button that is unclickable, but no message is shown to the visitor. If you uncheck that box, then the color/button is clickable, which then allows you to place a friendlier message about that variation being unavailable in the variation description field. But either way, the button or swatch is not hidden altogether, which is what I would prefer to have happen.
        I thought unchecking the Enable box on a variation would do the trick, but it does not.

  14. Bjornen says:

    About hiding out of stock (OOS) products when performing a search query, when using a filter or a widget such as product attribute and why it’s not possible as of right now.

    Or, maybe it is?

    1) Attributes (small, medium, large or red, green and blue) are terms.
    2) The terms are not directly tied to a ‘variation’, they are tied to a parent variable product.
    3) When querying products, they look at the parents only.
    4) They cannot look at the variations because the variations are not assigned to the term.
    5) Since the query cannot look at the variation, it cannot check its stock level and exclude it.

    As it stands, looping over all variations during queries would be a massive hassle.
    It would cause too large of a performance issue to warrant its inclusion.

    One possible idea is to assign terms to variations directly and to query those, but it will still require two queries during one run time; 1 for parents excl. variable and 1 for variations. Again, query heavy.

    If anyone out there has an idea on how to “fix” this, I’m more than happy to work with you for free on this.

  15. mark says:

    thank you . ive used the code in my website but there is question!
    products will be hide on google too ?
    i dont want to lose google search

Leave a Reply

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