Hide Out of Stock Items Exception in WooCommerce

WooCommerce stores with large inventory often decide to hide out of stock products from the website. 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.

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.

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”.

add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'hide_out_of_stock_exception_category' );
function hide_out_of_stock_exception_category( $hide ) {
if ( is_product_category( 'tables' ) ) {
$hide = 'no';
}
return $hide;
}

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 = 13.

add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'hide_out_of_stock_exception_page' );
function hide_out_of_stock_exception_page( $hide ) {
if ( is_page( 13 ) ) {
$hide = 'no';
}
return $hide;
}