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!
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']");
}
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!
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!
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 :
Thank you!
Thank you! This is great. Is it possible to get it to display the product name only? i.e. not the icon or price?
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:
.
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 !
Thanks a lot!
It might have stopped working, it shows me a list of product IDs instead of the products
Using single brackets instead of double seems to have fixed it.
Great!
You’re a genius, thank you!!!
Cheers!
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
Pity, works for me!
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!
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!
Thank you so much, But how to add pagination? because I have too many products…
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!
Hi, I would like pagination also and customization if possible
Hi Mihai, you can use the [[products]] shortcode parameters as per https://woocommerce.com/document/woocommerce-shortcodes/#products
Nice work. Cranks like a charm! Any chance of paginating such page this shortcode is on?
Or arrangement by type or something?
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!
Alexia,
Did you figure this out? I’m working through this now.
How to display “all” out of stock product even manage stock is not active?
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!
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?
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
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
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
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 🙂
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!
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
Hello Dave, thanks for your comment! You could try using “posts_per_page = -1” inside the $args to return all posts. Let me know 🙂
Hi Rodolfo,
When i try the shortcode, the products don’t appear, only the shortcode.
Can you help?
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 🙂
You’ve got to put the shortcode between brackets like so, [shortcode] – It works fine.
Hi
I have followed all the procedure but the out of stock products are not showing on front-end.
Hi there, thanks for your comment! Are all your products with “Manage stock” enabled?
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.
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
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!
Thank you Jerry, I’m emailing you in a few minutes 🙂
Hello
I’ve added the php code in my functions.php file but where do I place the shortcode?
Hey there – thanks so much for your comment! You can place a shortcode in a page, post, widget, etc 🙂
Does the still work with the latest woocommerce? 3.4.4?
Yes it should 🙂
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?
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
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?
Sorry, manage stock wasn’t ticked.
🙂
Any ideas on how to add a datestamp when I set a product to out of stock please?
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
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 ?
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 🙂
Thanks so much for this! I’ve used this code with the savoy theme and it works great!
Brilliant 🙂
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.
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
Hello, is it possible to hide “out of stock” products with tweaking on this code?
Hey Daniel, thanks so much for your comment! You can do so from the WooCommerce settings 🙂
Hi, thanks for this. Doesn’t appear to work with Woocommerce 3.3.3, using the Avada Theme
Hey Jay, it should work. Can you test with another theme please?
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
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
Hi,
only 10 products are displayed on my page, how do I set up to show all?
tx
Hey Damjan! Try adding this to the $args array:
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!
Great – awesome 🙂
I have “Hide out of stock items” enabled in WooCommerce > Settings.
Is this shortcode still show these products?
Please let me know. Thanks much!
Hey Lana! It should, did you try?
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!!
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!
Thank you very much, I will talk to my client and if so, I will contact you, Best Regards
Thanks!
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?
Great work, tnx so much!
Excellent, thanks for your feedback Jelena 🙂