skip to Main Content

My client sells single quantity products. Her site has a dedicated page for displaying those products once they are sold (out of stock). My challenge is to hide out of stock products from all other pages. So far I have added a number of filters. What I am trying to accomplish now is hide the out of stock products on the category pages. Is there a filter for that?

Here are the filters currently in use:

/**
 * @snippet       Display Out of Stock Products via Shortcode - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */
   
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
   
function bbloomer_out_of_stock_products_shortcode() {
 
   $args = array(
      'post_type' => 'product',
      'posts_per_page' => -1,
      'post_status' => 'publish',
      'meta_query' => array(
         array(
            'key' => '_stock_status',
            'value' => 'outofstock',
         )
      ),
      'fields' => 'ids',
   );
    
   $product_ids = get_posts( $args ); 
   $product_ids = implode( ",", $product_ids );
    
   return do_shortcode("[products ids='$product_ids']");
 
}

add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
function shop_only_instock_products( $meta_query, $query ) {
    // Only on shop archive pages
    if( is_admin() || is_search() || ! is_shop() ) return $meta_query;

    $meta_query[] = array(
        'key'     => '_stock_status',
        'value'   => 'outofstock',
        'compare' => '!='
    );
    return $meta_query;
}

/**
 * Hide loop read more buttons for out of stock items 
 */
if (!function_exists('woocommerce_template_loop_add_to_cart')) {
    function woocommerce_template_loop_add_to_cart() {
        global $product;
        if ( ! $product->is_in_stock() || ! $product->is_purchasable() ) return;
        wc_get_template('loop/add-to-cart.php');
    }
}

/* Hide out of stock products in related section */
add_filter( 'woocommerce_related_products', 'mysite_filter_related_products', 10, 1 );
function mysite_filter_related_products( $related_product_ids )
{

if (!is_admin()) {
    foreach ($related_product_ids as $key => $value) {
        $relatedProduct = wc_get_product($value);
        if (!$relatedProduct->is_in_stock() ) {
            unset($related_product_ids["$key"]);
        }
    }

    return $related_product_ids;
  }
}

2

Answers


  1. To hide out of stock products on a product archive page you need to modify the condition inside your woocommerce_product_query_meta_query hook.

    is_shop() condition only works for shop page, which is defined in WooCommerce > Settings > Products > General > Shop page. In your case you also need to add is_product_category() condition, to check if it’s a category page.

    Example:

    add_filter( 'woocommerce_product_query_meta_query', 'shop_only_instock_products', 10, 2 );
    function shop_only_instock_products( $meta_query, $query ) {
        // Only on shop archive pages
        if( is_admin() || is_search() || ( !is_shop() && !is_product_category() ) ) return $meta_query;
    
        $meta_query[] = array(
            'key'     => '_stock_status',
            'value'   => 'outofstock',
            'compare' => '!='
        );
        return $meta_query;
    }
    
    Login or Signup to reply.
  2. Add this code into the theme functions.php file and replace the wooCommerce category slug with your category slug.

    add_filter( 'woocommerce_product_query_meta_query', 'wine_hide_instock_products', 10, 2 );
        
        function wine_hide_instock_products( $meta_query, $query ) {
            
            if( is_product_category('warehouse-deals') ) {
                
                $meta_query[] = array(
                    'key'       => '_stock_status',
                    'value'     => 'outofstock',
                    'compare'   => '!='
                );
                
                return $meta_query;
            
            }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search