Display only "in stock" products in Woocommerce Recent Products shortcode
Asked Answered
M

4

5

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.

Maryannemarybella answered 30/8, 2017 at 17:25 Comment(0)
D
12

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.

Doily answered 30/8, 2017 at 18:33 Comment(0)
M
1

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);
Maryannemarybella answered 15/9, 2017 at 12:32 Comment(0)
T
1

Add Following code in your theme's functions.php file,

function custom_woocommerce_shortcode_products_query( $args ) {
   if ( 'yes' == get_option( 'woocommerce_hide_out_of_stock_items' ) ) {
     $args['meta_query'][] = array(
       'key' => '_stock_status',
       'value' => 'instock',
       'compare' => 'IN'
     );
   }
   return $args;
}

add_filter( 'woocommerce_shortcode_products_query', 'custom_woocommerce_shortcode_products_query' );
Timepiece answered 18/10, 2017 at 14:58 Comment(0)
H
0

The admin panel > WooCommerce > Settings > Products > Inventory

Check the "Hide out of stock items from the catalog" checkbox.

The original answer:

you can do this with the built in “Hide out of stock items from the catalog setting, which can be found at: [your_site]/wp-admin/admin.php?page=wc-settings&tab=products&section=inventory

Once this setting is enabled, out of stock products will be hidden from the shop catalog, as well as wherever an out of stock product is included in a shortcode.

Healing answered 5/9, 2022 at 8:20 Comment(1)
This would unfortunately hide all out-of-stock items across the entire shop which is not what I required. I only wanted to hide products without stock from the Recent Products Shortcode.Maryannemarybella

© 2022 - 2024 — McMap. All rights reserved.