You may want to hide all out of stock products (including variations added by our Show Single Variations plugin) from your WooCommerce catalog. In this post I’ll show you a couple of ways to do it; using your own code or using a global WooCommerce Setting. Take note of your version of WooCommerce, as the code based option is different between versions.

Hide Out of Stock products for WooCommerce 3.0.0 and above

WooCommerce changed the way visibility settings are applied to products in version 3.0.0 and above. As such, the code is slightly different to hide them from the catalog. If you’re using a version of WooCommerce greater than 3.0.0, then simply add the following snippet:

add_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );

function iconic_hide_out_of_stock_products( $q ) {

    if ( ! $q->is_main_query() || is_admin() ) {
        return;
    }

    if ( $outofstock_term = get_term_by( 'name', 'outofstock', 'product_visibility' ) ) {

        $tax_query = (array) $q->get('tax_query');

        $tax_query[] = array(
            'taxonomy' => 'product_visibility',
            'field' => 'term_taxonomy_id',
            'terms' => array( $outofstock_term->term_taxonomy_id ),
            'operator' => 'NOT IN'
        );

        $q->set( 'tax_query', $tax_query );

    }

    remove_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );

}

Product visibility is now handled in the form of a taxonomy, rather than post meta. So the above snippet will show all products as long as they are not set to outofstock. This includes product variations.

[alert title=”Psst! Want to show product variations on your shop pages?” background-color=”#DA2930″ color=”#FFFFFF” content=”Try the 14-day free trial of WooCommerce Show Single Variations and display product variations in your WooCommerce shop pages as though they were simple products.” button-text=”Get Free Trial” link=”https://iconicwp.com/products/woocommerce-show-single-variations/”]

Hide Out of Stock products for older WooCommerce versions

For older versions of WooCommerce (versions before 3.0.0), just add the following snippet:

add_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );

function iconic_hide_out_of_stock_products( $q ) {

    if ( ! $q->is_main_query() ) return;
    if ( ! $q->is_post_type_archive() ) return;
    if ( is_admin() ) return;

    $meta_query = (array) $q->get('meta_query');

    $meta_query[] = array(
        'key'       => '_stock_status',
        'value'     => 'outofstock',
        'compare'   => 'NOT IN'
    );

    $q->set( 'meta_query', $meta_query );

    remove_action( 'pre_get_posts', 'iconic_hide_out_of_stock_products' );

}

This just adds a new meta query to the main query that says only show products that don’t have an out of stock status. Sorted!

Hide Out of Stock products using the WooCommerce global setting

If you want less control, but a much more simple solution, you can just toggle the WooCommerce setting which will do all of this for you. Navigate to WooCommerce > Settings > Products > Inventory, and check Hide out of stock items from the catalog. Doing this will hide out of stock items everywhere on your site; in recently viewed widgets, shortcodes, search, catalog pages. Everywhere!