Issue
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.
Solution
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;
}
Answered By - AndRa Answer Checked By - Robin (PHPFixing Admin)
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.