Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display only "in stock" products in Woocommerce Recent Products shortcode

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.

like image 956
Elizabeth Drew Avatar asked Aug 30 '17 17:08

Elizabeth Drew


People also ask

How do you show stock items in WooCommerce?

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.

How do I display a particular category product in WooCommerce shortcode?

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.

How does WooCommerce show stock status on product page?

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.

How do I display out of stock items in WooCommerce?

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.


2 Answers

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.

like image 79
LoicTheAztec Avatar answered Sep 22 '22 19:09

LoicTheAztec


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);
like image 38
Elizabeth Drew Avatar answered Sep 24 '22 19:09

Elizabeth Drew