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
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.