The thing I want to accomplish is the following. In WooCommerce 3.9, Whenever I use the shortcode [product_category category="jeans"] or any other category I want it to show products of this category except the products which are out of stock
I know I can change the setting to hide all out of stock product in WooCommerce, but I want them to be shown at all places (archive, shop, widgets) except when I use the shortcode
I've tried codes like the one below (which i assume only works for [products category="jeans"] ) So that would also be an option
add_filter( 'woocommerce_shortcode_products_query' , 'tryout_exclude_oos_shortcodes');
function tryout_exclude_oos_shortcodes($query_args){
$query_args['tax_query'] = array(array(
'taxonomy' => 'product_visibility',
'field' => 'slug',
'terms' => array('outofstock'),
'compare' => 'NOT IN',
));
return $query_args;
}
At this moment I tried a lot, but my knowledge doesn't is limited at the moment. Is what I'm asking even possible and so.. what am I missing?
Give this a try:
add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop_name ){
if( $loop_name == 'product_category' ){
$query_args['meta_query'] = array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
) );
}
return $query_args;
}, 10, 3);
You can change the loop name to fit your application. Code goes in your child theme functions.php.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.