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
 * @community     https://businessbloomer.com/club/
 */

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
 * @community     https://businessbloomer.com/club/
 */

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 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: Show “Sold Out” @ Shop Page
    Here’s another simple snippet that can easily help user experience and make sure a “sold out” badge shows on each out of stock product in the category & shop pages. Not all themes allow this so you can use the snippet below to make it happen!
  • WooCommerce: Display Variations’ Stock @ Shop Page
    Thanks to the various requests I get from Business Bloomer fans, this week I’m going to show you a simple PHP snippet to echo the variations’ name and stock quantity on the shop, categories and loop pages. Of course, if “Manage stock” is not enabled at variation level, the quantity will be null, and therefore […]
  • WooCommerce: Display Out of Stock Products (Shortcode)
    A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all! You can use this shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof. So let’s see (1) […]
  • WooCommerce: Display Stock Availability @ Shop Page
    In this tutorial, my goal is to show the “stock availability” under each product in the shop, category and archive pages. This follows exactly the same settings as the stock display of the single product page. Go to /wp-admin/admin.php?page=wc-settings&tab=products&section=inventory to manage “Stock display format”. Enjoy!
  • 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, […]

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

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? 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 *