Home » WooCommerce: “Hide Out of Stock Items” Exception

WooCommerce: “Hide Out of Stock Items” Exception

by Tutor Aspire

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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'tutoraspire_hide_out_of_stock_exception_category' );

function tutoraspire_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 tutoraspire.com FREE
 * @author        Tutor Aspire
 * @compatible    WooCommerce 5
 * @donate $9     https://tutoraspire.com
 */

add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'tutoraspire_hide_out_of_stock_exception_page' );

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

You may also like