I need to exclude Out of Stock items from displaying when the Woocommerce Recent Products shortcode is used on my front page.
[recent_products]
Is it possible to create a rule like hide_outofstock="true"
or something along those lines to stop Out of Stock products showing?
I have trawled the web looking for ideas on how to approach this problem, and I'm no coder at all but usually I'm able to frankenstein something to get around issues like this. However, right now I am stumped. All and any help will be much appreciated.
I can't just hide all out of stock products via the WooCommerce settings page as they need to be visible in other areas of the site.
Using a code that "hides" rather than "doesn't pull" out of stock products just shows empty spaces where the products would have been shown.
Needs to work dynamically as stock levels change often - manually restricting by product id will take too long.
On the Settings page, go to the Products tab and then choose the Inventory tab. There are two options for showing stock: “Hide out of stock items from the shop page”: If you check this box, WooCommerce will hide any products that are out of stock from your shop page.
These two shortcodes will display your product categories on any page. [product_category] – Will display products in a specified product category. [product_categories] – Will display all your product categories.
If the 'Stock display format' option on WooCommerce → Settings → Products → Inventory is set to display the quantity remaining in stock, then every product will have detailed stock information. In stock products will display the exact number remaining in stock.
Use the Out of Stock Visibility Option To change this, go to WooCommerce > Settings > Products > Inventory and change the “Out of Stock Visibility” setting to “Show.” This will make sure that when a product is out of stock, it will still be visible on your shop pages.
June 2018 Update (for product type compatibility)
After a little search in WC_Shortcodes
class source code, here is the proper way to do it:
add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop_name ){
if( $loop_name == 'recent_products' ){
$query_args['meta_query'] = array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
) );
}
return $query_args;
}, 10, 3);
Code goes in function.php file of your active child theme (or theme) or also in any plugin file.
Tested on WooCommerce 3+ and works.
Just a small update. The above code worked well with simple products, but variations with one variation in stock and another variation out of stock didn't show when using the [recent_products] shortcode. I think I have fixed this by changing the value to outofstock and compare to NOT LIKE.
add_filter( 'woocommerce_shortcode_products_query', function( $query_args, $atts, $loop_name ){
if( $loop_name == 'recent_products' ){
$query_args['meta_query'] = array( array(
'key' => '_stock_status',
'value' => 'outofstock',
'compare' => 'NOT LIKE',
) );
}
return $query_args;
}, 10, 3);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With