4

I am trying to hide out of stock products only from Shop page, but keep them on the separate category page.

Woocommerce settings allows only to choose either to show out of stock products or to hide them completely.

How to hide out of stock products only on shop archive pages in Woocommerce?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
Rtangle
  • 73
  • 1
  • 7

1 Answers1

6

Updated: Using a custom function hooked in woocommerce_product_query_meta_query filter hook, targeting non "out of stock" products on shop archive pages only:

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;
}

This code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Hi, thank you for your reply. Honestly, I am a bit new to editing php in Wordpress and new in php overall. I have pasted this code in functions.php within the child theme, but I get the following errors. http://prntscr.com/hot6mv – Rtangle Dec 17 '17 at 21:32
  • Thanks, that worked :) but now I have other problem. I am using YITH Product Filter, and it works on Shop page, but when I go to category page in which I have out of stock products, and if I choose a filter, no products are displayed. I found some code for that issue but it doesn't work. add_filter ('yith_wcan_use_wp_the_query_object', '__return_true'); – Rtangle Dec 17 '17 at 21:46
  • 1
    Ok, sorry for that. Thanks :) I'll add new question. – Rtangle Dec 17 '17 at 21:51
  • Didn't work for me, I just post solution on https://stackoverflow.com/a/72424314/3767185 – vahid sabet May 29 '22 at 14:03