0

i have been searching for hours but to no avail, i have tried combining codes that work

1.Hiding out of stock products on shop page 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;
}

with

  1. hiding products based on product category:

    add_filter( 'woocommerce_product_query_tax_query', 'custom_product_query_tax_query', 10, 2 );
    function custom_product_query_tax_query( $tax_query, $query ) {
    if( is_admin() ) return $tax_query;
    
    // HERE Define your product category SLUGs to be excluded
    $terms = array( 'ukategorisert' ); // SLUGs only
    
    // The taxonomy for Product Categories
    $taxonomy = 'product_cat';
    
    $tax_query[] = array(
    'taxonomy' => $taxonomy,
    'field'    => 'slug', // Or 'name' or 'term_id'
    'terms'    => $terms,
    'operator' => 'NOT IN', // Excluded
    );
    
    return $tax_query;
    }
    

and tried combining them to become the code below but it didn't work out. :

    add_filter( 'woocommerce_product_query_meta_query', 'prodcat', 10, 2 );

    function prodcat( $meta_query, $query ) {

  // HERE Define your product category SLUGs to be excluded
    $terms = array( 'pouch' ); // SLUGs only

    // The taxonomy for Product Categories
    $taxonomy = 'product_cat';

    $tax_query[] = array(
    'taxonomy' => $taxonomy,
    'field'    => 'slug', // Or 'name' or 'term_id'
    'terms'    => $terms,
    'operator' => 'NOT IN', // Excluded
);

return $tax_query;

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


}

please shed some of your guidance and knowledge to help hide out of stocks products based on product category.

thank you so much in advance.

AndRa
  • 1
  • 4
  • 1
    Please provide more details of what goes wrong, and distill your problem into a simple example: https://stackoverflow.com/help/minimal-reproducible-example – Ganesh Sittampalam Aug 12 '19 at 21:20
  • woocommerce default settings to hide out of stock products apply to everything, including variation drop downs. with a custom code, basically i am trying to hide out of stock products based on the product category of my choice. the code that i tried combining did not produce any result. it does not hide any out of stock products at all in fact. i am still learning to code, so the code i tried combining must surely not affect anything since it does not produce any result. – AndRa Aug 13 '19 at 04:49
  • You should edit your question with updates - take a good look at the link I provided for advice on how to get a good answerable question. At the moment it doesn't make much sense. – Ganesh Sittampalam Aug 13 '19 at 12:13

1 Answers1

0

i didnt realise that my question may be ambiguous or unclear. but well i tweaked around the code and managed to get the same result that i wanted, i.e. hiding out of stock products not globally but based on product category.

this is done using the code from @loictheaztec. his code :

    add_action( 'woocommerce_before_add_to_cart_form', 'woocommerce_sold_out_dropdown' );

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

his code above was to hide out of stock products only on shop page (reference: Hide out of stock products only on shop archive pages in Woocommerce)

basically, i used his code to hide the out of stocks products based on a specific page and on the specific page, i used shortcodes from woocommerce to display a specific product category. so there you go, i have managed to hide out of stock products based on product category. although i must admit it was kind of beating around the bush in order to get to the goal. but well, as long as the work gets done. :)

so here is the code:

    add_filter( 'woocommerce_product_query_meta_query', 'prodcat', 10, 2 );

    function prodcat( $meta_query, $query ) {

 if( is_admin() || is_search() || ! is_page( 9173 ) ) return $meta_query;

// is_page ( 'id of page you want to hide the out of stock products, use array() for multiple pages') //

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


    }
AndRa
  • 1
  • 4