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 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;}
WooCommerce: “Hide Out of Stock Items” Exception .