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!
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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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 businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @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;
}
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
Hi Rob, take a look at https://developer.wordpress.org/reference/functions/is_page/, it can accept an array of IDs as well
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?
I honestly think this is not related to my snippet?
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 π
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!
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?
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!
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())) {
Nice
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:
I would like to understand what I am doing wrong.
Thanks a lot!
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…
Hello Jean-Marie, I suggest you take a look at “conditional logic”: https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Enjoy π
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?
Hi Jesus, have you tried adding more categories inside the snippet?
Hi Rodolfo,
Thanks for the info.
I’ve tried adjusting Override Out of Stock Visibility Setting On a Specific WordPress Page to
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.
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!
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
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
Great!
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.
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!