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 shortcode for different goals. For example, you might want to display what products you’ve sold to enhance customer trust / social proof.

So let’s see (1) how to create a shortcode and (2) how to take advantage of the existing [products] WooCommerce shortcode and its “ids” parameter to pass just those product IDs that are out of 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] on any page.

In order for this to work, “Hide out of stock items from the catalog” must be disabled in the WooCommerce settings, otherwise the shortcode will return nothing.

/**
 * @snippet       Display Out of Stock Products via Shortcode - WooCommerce
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 5
 * @community     https://businessbloomer.com/club/
 */
  
add_shortcode( 'out_of_stock_products', 'bbloomer_out_of_stock_products_shortcode' );
  
function bbloomer_out_of_stock_products_shortcode() {

	$args = array(
		'post_type' => 'product',
		'posts_per_page' => -1,
		'post_status' => 'publish',
		'meta_query' => array(
			array(
				'key' => '_stock_status',
				'value' => 'outofstock',
			)
		),
		'fields' => 'ids',
	);
	
	$product_ids = get_posts( $args ); 
	$product_ids = implode( ",", $product_ids );
	
	return do_shortcode("[products ids='$product_ids']");

}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Display All Products Purchased by User
    When a WooCommerce customer is logged in, you might want to show them the list of previously purchased products (maybe in a custom “My Account” tab). This is helpful when customers tend to buy the same products over and over again, and therefore you can help them “order again” without having them to search the […]
  • WooCommerce: Show “Sold Out” @ Shop Page
    Here’s another simple snippet that can easily help user experience and make sure a “sold out” badge shows on each out of stock product in the category & shop pages. Not all themes allow this so you can use the snippet below to make it happen!
  • WooCommerce: Display Stock Availability @ Shop Page
    In this tutorial, my goal is to show the “stock availability” under each product in the shop, category and archive pages. This follows exactly the same settings as the stock display of the single product page. Go to /wp-admin/admin.php?page=wc-settings&tab=products&section=inventory to manage “Stock display format”. Enjoy!
  • WooCommerce: Display Variations’ Stock @ Shop Page
    Thanks to the various requests I get from Business Bloomer fans, this week I’m going to show you a simple PHP snippet to echo the variations’ name and stock quantity on the shop, categories and loop pages. Of course, if “Manage stock” is not enabled at variation level, the quantity will be null, and therefore […]
  • WooCommerce: Display “In Stock” Products First @ Shop
    We’ve already seen how to add a custom “Product Sorting” option to the “Default Sorting” dropdown in the WooCommerce Shop page. The task I was presented with, however, was to display items based on a custom “meta key”. Now, if you have no idea what a “meta key” is, don’t worry too much. For example, […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

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

  1. Great work Rodolfo!

    Is it possible in Woocommerce if a product stock reach 0 move it automatically from A category to B category? I searched the web around but didnt found any solution for this. Best Regards!

    1. Hi Peter, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  2. Thanks for sharing !
    Works fine with div and woocommerce 6.5.1.
    I tried to have the same with backorder products, it finally worked out with the following code :

    add_shortcode( 'on_backorder_products', 'bbloomer_backorder_products_shortcode' );
       
    function bbloomer_backorder_products_shortcode() {
     
       $args = array(
          'post_type' => 'product',
          'posts_per_page' => -1,
          'post_status' => 'publish',
          'meta_query' => array(
             array(
                'key' => '_stock_status',
                'value' => 'onbackorder',
             )
          ),
          'fields' => 'ids',
       );
        
       $product_ids = get_posts( $args ); 
       $product_ids = implode( ",", $product_ids );
        
       return do_shortcode("[products ids='$product_ids']");
     
    }
    
      1. Thank you! This is great. Is it possible to get it to display the product name only? i.e. not the icon or price?

  3. I couldn’t find this information anywhere! Like your client I wanted to show products on a separate page depending on their stock status! In this case products in stock so I just change the snippet a bit:

    /**
     *Display In Stock Products via Shortcode - WooCommerce
     * @how-to        Get CustomizeWoo.com FREE
     * @author        Rodolfo Melogli
     * @compatible    WooCommerce 5
     * @community     https://businessbloomer.com/club/
     */
       
    add_shortcode( 'in_stock_products', 'in_stock_products' );
       
    function in_stock() {
     
       $args = array(
          'post_type' => 'product',
          'posts_per_page' => -1,
          'post_status' => 'publish',
          'meta_query' => array(
             array(
                'key' => '_stock_status',
                'value' => 'instock',
             )
          ),
          'fields' => 'ids',
       );
        
       $product_ids = get_posts( $args ); 
       $product_ids = implode( ",", $product_ids );
        
       return do_shortcode("[products ids='$product_ids']");
     
    }

    .

    That said I think you should include on this blog post the actual shortcode [product_out_of_stock_products] to copy and paste to make it easy for the people that don’t know code (like me)

    Thank you ! This is great !

  4. It might have stopped working, it shows me a list of product IDs instead of the products

    1. Using single brackets instead of double seems to have fixed it.

      return do_shortcode("[products ids='$product_ids']");
  5. You’re a genius, thank you!!!

  6. I tired your code but it didn’t work for me
    It displays all of the products .. it doesn’t apply out of stock filtration

    1. Pity, works for me!

  7. Hi there
    This is working perfectly in terms of displaying out of stock items where I want them to appear.
    I did wonder if there is a line or 2 I can add which would mean it displays product by date? It seems the default display (for me at least) is alphabetical.
    Thank you so much!

    1. Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  8. Thank you so much, But how to add pagination? because I have too many products…

    1. Hi Sohel, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

      1. Hi, I would like pagination also and customization if possible

        1. Hi Mihai, you can use the [[products]] shortcode parameters as per https://woocommerce.com/document/woocommerce-shortcodes/#products

  9. Nice work. Cranks like a charm! Any chance of paginating such page this shortcode is on?

    Or arrangement by type or something?

    1. Hi there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  10. Alexia,

    Did you figure this out? I’m working through this now.

  11. How to display “all” out of stock product even manage stock is not active?

    1. Hello Dewi, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  12. hey there i want to show out of stock products but it is not showing i have checked all the setting but the settings are not applying what should i do
    please any one can help?

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

  13. Hi
    I have followed all the procedure but the out of stock products are not showing on front-end. It shows only the shortcode
    Thanks for your help

    1. Hi Martin, thanks so much for your comment! I just retested this on the latest version of WooCommerce and it still works. Unfortunately this looks like custom troubleshooting work and I cannot help here via the blog comments. Thanks a lot for your understanding! ~R

      1. Hi thanks its working fine on a page but out of stock products still showing on Shop and Categories Pages. I just want to show out of stock products on a page where I used your shortcode. TIA 🙂

        1. Hi Ibtesam, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

  14. Hi Rodolfo,

    I have used the code and it worked fine. However, it displays only 10 products on the page.
    I made sure to keep the “Manage Stock” enabled and kept the stock quantity at 0 and still not getting the newly “Sold Out” products on the page.

    Any suggestions? Thanks a lot in adv. 🙂

    Regards,
    Dave CJ

    1. Hello Dave, thanks for your comment! You could try using “posts_per_page = -1” inside the $args to return all posts. Let me know 🙂

  15. Hi Rodolfo,
    When i try the shortcode, the products don’t appear, only the shortcode.
    Can you help?

    1. Hi there, thanks for your comment! The shortcode shouldn’t appear – please enter it via the HTML (text) editor and not the “visual” one. Let me know 🙂

    2. You’ve got to put the shortcode between brackets like so, [shortcode] – It works fine.

  16. Hi
    I have followed all the procedure but the out of stock products are not showing on front-end.

    1. Hi there, thanks for your comment! Are all your products with “Manage stock” enabled?

  17. 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?
    Thanks.

    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

  18. 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 🙂

  19. 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 🙂

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

    1. Yes it should 🙂

  21. 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

  22. 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.

      1. 🙂

  23. 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

  24. 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 🙂

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

  26. 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 https://businessbloomer.com/web-design-quote/ and I’ll get back to you as soon as I can. Thanks in advance! R

  27. 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 🙂

  28. 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?

  29. 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
    Martin

    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

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

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

      'posts_per_page' => -1,
      
  31. 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 🙂

  32. 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?

      1. Hi, the shortcode is awesome but don´t work if the option “Hide out of stock items from the catalog” is enabled, is possible show the sold out product with the shortcode if that opcion is enabled? thank you!!

        1. Hi Eduardo, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

          1. Thank you very much, I will talk to my client and if so, I will contact you, Best Regards

            1. Thanks!

    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?

  33. Great work, tnx so much!

    1. Excellent, thanks for your feedback Jelena 🙂

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

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