WooCommerce: “Hide Out of Stock Items” Exception

WooCommerce stores with large inventory often decide to hide out of stock products from the website. As you all know, there is a WooCommerce setting for that, right under Settings > Products > Inventory called “Out of stock visibility“. With the tick of a checkbox you can toggle the visibility of products that ran out of stock and immediately return a clean shop page with no unpurchasable items.

The story is, it could be you may want to still show out of stock items on a specific page via a custom shortcode, or limit the out of stock visibility setting only to certain categories.

Well, today we will learn a cool WordPress hook called “pre_option_option“, that basically allows us to override whatever settings we have in the WordPress admin, and assign our own value on a specific page or condition. Enjoy!

From WooCommerce Settings > Products > Inventory you can set “Out of stock visibility” to “Hide out of stock items from the catalog” in order to not display unavailable products in the shop. In this tutorial, we’ll see how to override / make an exception to this given a specific condition or on a specific page.

PHP Snippet 1: Override Out of Stock Visibility Setting On a Specific WooCommerce Product Category Page

Given for granted you opted to “Hide out of stock items from the catalog” by ticking the checkbox in the settings, in this case scenario we don’t want to hide out of stock products for product category “tables”.

/**
 * @snippet       Hide Out of Stock Exception @ Category Page
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'bbloomer_hide_out_of_stock_exception_category' );

function bbloomer_hide_out_of_stock_exception_category( $hide ) {
	if ( is_product_category( 'tables' ) ) {
		$hide = 'no';
	}	
	return $hide;
}

PHP Snippet 2: Override Out of Stock Visibility Setting On a Specific WordPress Page

Given for granted you opted to “Hide out of stock items from the catalog” by ticking the checkbox in the settings, in this case scenario we don’t want to hide out of stock products on page ID = 123.

/**
 * @snippet       Hide Out of Stock Exception @ Page
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */

add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'bbloomer_hide_out_of_stock_exception_page' );

function bbloomer_hide_out_of_stock_exception_page( $hide ) {
	if ( is_page( 123 ) ) {
		$hide = 'no';
	}	
	return $hide;
}

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.

22 thoughts on “WooCommerce: “Hide Out of Stock Items” Exception

  1. Hi

    So if I wanted some products to display on some category pages, then should I use this element

    is_page( 123 ) and change to is_page( 123, 1234,5821 )

    Is it just a comma i need to state which categories/pages I want the Out of Stock product to display?

    Cheers
    Rob

  2. Hi Rodolfo, congratulations for these notions you provide.
    I activated the snippet Hide Out of Stock Exception @ Category Page, it works great but checking the debug.log this warning appears:

    PHP Notice: Function is_tax was called incorrectly. Conditional query tags do not work before the query is run. Before then, they always return false. Please see Debugging in WordPress for more information. (This message was added in version 3.1.0.) in wp-includes/functions.php on line 5835
    What do you think it could be?

    1. I honestly think this is not related to my snippet?

  3. Wow, works pretty well, I’m wondering how to do the opposite, to hide Out of stock product on only certain category.
    Is that possible?

    Thank you 🙂

    1. Hello Loic, 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! You have amazing content, always super helpful! I’m trying to hide the sold-out products on the shop and categories pages, but have them show in the search results. Is there any way I can adapt this shortcode to accomplish that?

    1. Hi Vanusa, 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!

  5. Still Works like Charm!

    thanks for this

    the only change i did was this line for all archives to show sold out products

    if (is_woocommerce() && (is_shop() || is_product_category() || is_product_tag() || is_product_taxonomy())) {

      1. Hi,

        The code is great!! I tried to use this part of the code, and it works to show all the products, but I would like to define for example only products that have producttag “lookbook”. I’m probably doing something wrong because it doesn’t work.
        The code I used is as follows:

        if (is_woocommerce() && is_product_tag( lookbook )) {  

        I would like to understand what I am doing wrong.

        Thanks a lot!

        1. I am looking for the exact same thing! To hide all out of stock products except those with a certain tag!
          I found some hooks like ‘has_tag’ or ‘has_term’ but I can’t get it to work. I have either all products visible or none. Or all products in a certain category. But they should be visible everywhere. Not only when viewing that category archive…

          1. Hello Jean-Marie, I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy 🙂

  6. Hi there,

    The snippet works fine but only works in specific category pages. I have products with 2 categories (only one of them is on the snippet to get the exception) and when i on the non-exception page, the product are hidden.

    I need to display this exception on all catalog pages. Is not possible?

    1. Hi Jesus, have you tried adding more categories inside the snippet?

  7. Hi Rodolfo,
    Thanks for the info.
    I’ve tried adjusting Override Out of Stock Visibility Setting On a Specific WordPress Page to

    $hide = 'yes'

    for a specific page/post ID on a ‘bundled product’ tied to a subscription and it doesn’t want to play nicely. Have you had any experience with anything similar to this?
    I’m trying to hide out-of-stock products from a bundled product because it is confusing the customers, but if I literally remove the out-of-stock product from the bundle then when subscriptions renew blocker issues occur (not your problem).
    I was hoping for a visual fix to a WooCommerce plugin integration issue that WC doesn’t want to help with.

    1. Hi Jason 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!

  8. Hi, thanks for sharing this useful resource.
    I’m trying to hide the sold out products EXCEPT in a category page (I created a category called “sold”).
    Using your code I can not show them only in that category page, maybe because that’s not the page id but the category id?
    The ID I can find is like:
    https://www.domain.com/wp-admin/term.php?taxonomy=product_cat&tag_ID=1048&post_type=product….etc…..
    Could you please share if there is a way to achieve the result i’m expecting?

    Your help would be much appreciated.
    Have a nice day,
    Simone

    1. Sorry I just found the first snippet with the category name works perfect for me!
      🙂
      I’m subscribing to your content that is very useful! Thanks,
      Simone

        1. WoW! I’ve been searching for this for ages and it’s perfect for my scenario.

          We sell fireworks, many people like to look back and search for their old favourite fireworks from years past. We set up a Firework Archive on our website, but couldn’t get the products to display as they’re all out of stock. This solves that issue.

          One a separate note, what do I need to do to show all sold out products in sub categories or multiple categories?

          Many thanks for this.

          1. Hey Mark 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!

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 *