Posted on

WooCommerce: Display Out of Stock Products (Shortcode)

A client of mine wanted to show out of stock products on a separate page – so I coded a simple shortcode for you all!

You can use this same shortcode for different goals. For example, you might want to display what products you’ve sold so far to enhance customer trust (which is the same as displaying those products that are not in stock). Or maybe, you might want to have a specific page where customers can go to see what’s coming back in stock (if you don’t sell unique pieces and restock your products).

Either way, let’s see how to (1) create a shortcode and (2) how to use a WordPress query to return just those products that have 0 stock!

WooCommerce: Show Out of Stock Products via a Shortcode

PHP Snippet: Display Out of Stock Products via a Shortcode – WooCommerce

After you add the PHP below to your website, you can use the shortcode “out_of_stock_products” (see image) on any page you like.

Please note – all your products must be set to “Manage Stock”.

 * @snippet       Display All Products Out of Stock via a Shortcode - WooCommerce
 * @how-to        Watch tutorial @
 * @sourcecode
 * @author        Rodolfo Melogli
 * @compatible    Woo 3.3.4
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
function bbloomer_out_of_stock_products_shortcode() {
global $product, $woocommerce, $woocommerce_loop;
$columns = 4;

$args = array(
    'post_type'             => 'product',
    'post_status'           => 'publish',
    'meta_query'            => array(
            'key'           => '_stock',
            'value'         => 1,
            'compare'       => '<'
$loop = new WP_Query($args);
while ( $loop->have_posts() ) : $loop->the_post();
wc_get_template_part( 'content', 'product' );
return '<div class="woocommerce columns-' . $columns . '">' . ob_get_clean() . '</div>';

Where to add this code?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme style.css file. Make sure you know what you are doing when editing such delicate files - if you need more guidance, please take a look at my free WooCommerce Customization video tutorial.

Does this snippet still work?

Please let me know in the comments if everything worked as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting on PHP 7.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce?

Check out these free videos, tutorials and tips!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP
  • woocommerce-customize-shop-page-PHP
  • woocommerce-advanced-customization
  • how-to-edit-woocommerce-cart
  • woocommerce-customize-checkout-page-PHP
  • woocommerce-email-customization
  • woocommerce-conditional-logic

Rodolfo Melogli

Author, WooCommerce expert, WordCamp speaker and Internet marketer, Rodolfo Melogli has worked as a WooCommerce freelancer since 2011. He helps entrepreneurs and developers overcome their WooCommerce nightmares :) Rodolfo is the organiser of WordCamp Dublin, the Dublin WooCommerce Meetup, the Dublin Ecommerce Meetup and the Dublin WordPress Meetup. He enjoys interacting with people, travelling and chasing tennis & soccer balls. Of course, he loves pizza too.

36 thoughts on “WooCommerce: Display Out of Stock Products (Shortcode)

  1. Hello Rodolfo,
    your snippet works fine. Thanks you.
    I see one improvement that I’m not able to do.
    I would like to have a pagination at the bottom of the loop.
    I saw that woocommerce add a new attribute to their shortcode : paginate=”true”
    Is there a way to include this option to your snippet?

    1. Hello Franck, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  2. Mr. Melogli

    I’m having troubles getting my Out Of Stock products to appear at the end of my WooCommerce results/shop pages while leaving the In Stock items in their current position/sorting.

    I’ve tried to copy/paste snippets now for the better part of two hours.

    Can you please contact me (via email), if you’re able to help me with this issue? I’m sure you can manage to sort it quickly but this coding goes beyond my scope of knowledge; I just keep mucking up my functions.php with syntax errors.

    Happy to compensate you for your time. If you’re interested, please email me with any concerns and/or a bid for the coding.

    Thank you for your time!

    1. Thank you Jerry, I’m emailing you in a few minutes πŸ™‚

  3. Hello

    I’ve added the php code in my functions.php file but where do I place the shortcode?

    1. Hey there – thanks so much for your comment! You can place a shortcode in a page, post, widget, etc πŸ™‚

  4. Does the still work with the latest woocommerce? 3.4.4?

    1. Yes it should πŸ™‚

  5. All seemed fine, even got it showing the dozens of out of stock items. But I have to have β€œHide out of stock items” enabled in WooCommerce > Settings.which stops your shortcode showing anything. Am I stuck?

    1. Patrick, with PHP there is always a plan B. Unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  6. This doesn’t seem to work for me using Storefront using “Code snippets”. I get nothing listed despite having unchecked “Hide out of stock items from the catalog” even though I need to do that.

    Before I faff around with a childe theme can you tell me that, if this worked, would it show out of stock items if the woo setting was to hide them?

    1. Sorry, manage stock wasn’t ticked.

    2. πŸ™‚

  7. Any ideas on how to add a datestamp when I set a product to out of stock please?

    1. Hey Craig, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  8. Hi, Hide out of stock items via the woocomerce would also hide it from google ? and also from links on other sites to your products ? It is an almost useless functionality of woocomerce. Can your code hide out of stock items for certain display purposes on the site, like search or product category or shop ? but keep it live via the link ?? so that seo isn’t killed in the process ?

    1. Hey Jason, thanks so much for your comment! This snippet displays the out of stock products, so I believe your question is a little off topic. Hiding the out of stock products does not mean removing them from the database though, so I think Google still sees them πŸ™‚

  9. Thanks so much for this! I’ve used this code with the savoy theme and it works great!

  10. Hi,

    I would like to get a version of this code the would work with the current version of Avada. I would be happy to pay you. How might I go about that?

    Thanks so much.

    1. Dear Ken, thanks so much for your comment! Please post your job specs at and I’ll get back to you as soon as I can. Thanks in advance! R

  11. Hello, is it possible to hide “out of stock” products with tweaking on this code?

    1. Hey Daniel, thanks so much for your comment! You can do so from the WooCommerce settings πŸ™‚

  12. Hi, thanks for this. Doesn’t appear to work with Woocommerce 3.3.3, using the Avada Theme

    1. Hey Jay, it should work. Can you test with another theme please?

  13. Hello Rodolfo

    I wonder if you could give your advice on this problem. I run a shop for a plant nursery where products are always dropping in and out of stock. Currently if you select a product category ( eg Irises ) where there are no currently available plants you get a blank screen with the following message:
    ‘No products were found matching your selection.’
    – which is a bit stark and unfriendly. Is is possible to customise the text here so that I can say something a bit more positive, like ‘ Sorry, there are no plants currently available in this category. However, plant availability is constantly changing so please try again soon.’
    thank you

    1. Hello Martin, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  14. Hi,
    only 10 products are displayed on my page, how do I set up to show all?

    1. Hey Damjan! Try adding this to the $args array:

      'posts_per_page' => -1,
  15. Wow works like a charm! Tested on Bridge theme. And indeed don’t forget to turn on ‘manage stock’.
    Maybe you should make a little plugin of this for Visual Composer?

    TY so much for this code!

    1. Great – awesome πŸ™‚

  16. I have “Hide out of stock items” enabled in WooCommerce > Settings.
    Is this shortcode still show these products?
    Please let me know. Thanks much!

    1. Hey Lana! It should, did you try?

    2. Thanks for sharing this. But the short code doesn’t work when “Hide out of stock items” is enabled. Is there a way to make it work?

  17. Great work, tnx so much!

    1. Excellent, thanks for your feedback Jelena πŸ™‚

Questions? Feedback? Support? Leave your Comment Now!
If you're writing code, please wrap it between: [php] code_here [/php]

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.