I’ve seen many snippets that change the “In Stock” text on the single product page, but not the FULL string. In this particular case, not only I needed to change the text, but also edit the order of display: from “2 in stock” to “Quantity: 2“.
PHP Snippet: Change “# in stock” to “Quantity: #” @ WooCommerce Single Product Page
/**
* @snippet Display "Quantity: #" @ WooCommerce Single Product Page
* @how-to businessbloomer.com/woocommerce-customization
* @author Rodolfo Melogli, Business Bloomer
* @testedwith WooCommerce 4.4
* @community https://businessbloomer.com/club/
*/
add_filter( 'woocommerce_get_availability_text', 'bbloomer_custom_get_availability_text', 99, 2 );
function bbloomer_custom_get_availability_text( $availability, $product ) {
$stock = $product->get_stock_quantity();
if ( $product->is_in_stock() && $product->managing_stock() ) $availability = 'Quantity: ' . $stock;
return $availability;
}
Adapting what Mark did, this time in order to show the stock message only to a given category even when we are adding stock management to all products. In my case, I needed that information only for marketplaces feed, while on a specific product tag there’s actual stock on the shop thus making it worth to show that info on the frontend.
Here’s how I did it:
Nice!
Hi,
thanks for this it was just what I needed. I added a couple of things with the help of some of the comments and other posts.
This displays ‘Sorry, I am sold out!’ when out of stock.
Nothing for any stock value 2 or higher.
‘Only 1 left!’ when stock is 1. For this message it also excludes a category, in 2 languages, as I am using WPML. This could be any array of categories though. I did not want ‘Only 1 left!’ splashed all over the Original Artworks page as there is always only 1 of each.
Adding the global $product variable seemed to be necessary to get category exclusion to work.
I also added the code from one of your other posts to make this appear on category pages as well as in the single product pages.
I can patch together bits of PHP, I am no expert. I would be very interested in any tips from anyone to make this neater, more efficient, shorter etc.
Great!
If Enable stock management at product level is not checked i would like to show some message in product page for Stock status when Stock status is in stock ” Available” , when outofstock “Out of stock” and when on backorder “Available after 4-7days” is it possible
Hey Giorgio, 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!
Is there a similar way to show how many people have placed an item in their cart?
We have one of most items and I want people to know if there is the potential of someone else buying it.
Thanks!
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!
Hi!. I want to change stock status displayed on product page based on shipping classes? can someone help? Thanks
Hello Sarah, 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!
I used this to make a snip that only shows quantity on items with a stock of 2 or more.
Thank you so much for all your time posting these, and to all the people submitting their tweaks and solutions.
Thank you!
Hi, i cannot seem to make it work.
I used your code but i need to make a tweak with custom message and decimal stock.
($stock >= 5.1) -> message More then 5 in stock
($stock = 5) -> message 5 in stock
($stock message Less then 5 in stock
But it doesnt seems to work for me.
What doesn’t work exactly? Do you see anything on the screen or nothing at all?
Odd, this isn’t working in any theme I throw it in.
Snippet revised, it should work now 🙂
This worked for me but it is displaying under the product description, how do I get it to display the stock quantity next to the price?
Hello Tracey, 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!
Still works. Running latest WordPress and Woocommerce. THANK YOU!
Awesome!
Hi,
How do i only show this on the single product pages?
It is now also showing on the images in the product loop and on the images in the Related products.
Kind regards,
Sven Heinen
Hello Sven, thanks for your comment! First of all, I’ve revised the snippet to make it compatible with latest WooCommerce. Also, in regard to your question, first try the snippet and if that doesn’t fix your issue, then your theme or another plugin are adding that to the loop, and not default WooCommerce.
Exactly what i was looking for BUT it does not get the right quantity. I am using it on Variable Subscription products. I use the woo-subscriptions plugin.
Hello Naresh, 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
What if we wanted to do this on a variable product? So it only shows the stock for the selected variant rather than the total of all the varients?
Hey Jay, 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
A question, if you can help me:
currently in the product page on Inventory (list)
I have: stock, stock exhausted and pre-order. I would like to add and “soon, let me know when it arrives” Can this be done?
Excuse me if I did not express myself well, but I use google translation.
Thanks ant
Hello there, 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
This is kinda an old post, I am looking to do this to a specific product category. Using slug or ID or however it can be done. That way depending on product category it can say something else. Like selling classes.
“3” Seats available.
and classes will be under a product category called Classes.
Hey Sean, thanks for your comment! I suggest you take a look at “conditional logic”: https://businessbloomer.com/conditional-logic-woocommerce-tutorial/ and https://businessbloomer.com/woocommerce-conditional-logic-ultimate-php-guide/. Let me know 🙂
Hello,
I’m looking for a solution to solve the following issue:
I sell products from two brands. Each brand has different delivery times.
When I have the product in stock, I can deliver in 1-3 days UK, and 2-5 days Europe.
When I don’t have the product in stock, I allow backorders. However, estimated delivery for brand A is 2-4 weeks, and brand B is 4-6 weeks.
So basically I would like to add a condition based on the “brand” attribute to change the Backorder text.
Thanks!
Hello Rafael, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide you with a complementary solution here on the blog. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R
Hi Rafael,
I had the same question from a client a couple of weeks back. They and I found it to be very helpful and I thinks it’s a solution that you might find fitting.
https://codecanyon.net/item/woocommerce-availability-notifications/8422290
Thanks alot for this one!
How about if you want to change the ”avaliable on backorder” text on variable products? Would be awesome if you could help out! Sincerely, Vic
Hey Vic, thanks for your comment! If you want to change a single string of text, check this article out: https://businessbloomer.com/translate-single-string-woocommerce-wordpress. Hope this helps!
Hi Rodolfo,
Great post!
I was wondering what I would need to change if I would need a third message instead of two different messages (1 for in stock items and 1 for Out of stock).
E.g.
0 stock = Out of Stock
1-10 stock = Only X items left, hurry!
>10 stock = Quantity: X items
Any help would be appreciated!
Thanks in advance
Koen
Koen, thanks for your comment! Your query is very simple – you just need to use an additional PHP if_then statement:
Let me know 🙂
Thanks Rodolfo 🙂
I found lots of snippets to change the text to a fixed ‘in stock’ or ‘out of stock’ message, but was looking for a way to include the actual stock level too.
In my case, I wanted:
// Change stock message for ‘in stock’ products
if ( $_product->is_in_stock() ) $availability[‘availability’] = __(‘In stock (‘ . $stock . ‘ available)’, ‘woocommerce’);
This ‘Code Snippets’ plugin is a great way to easily add these bits of code without having to mess around with altering any php files:
https://wordpress.org/plugins/code-snippets/
Regards, James
Thank you for your feedback James 🙂
Thankyou Very Much Sir Code is working perfectly
Awesome Yasir, thanks so much for the feedback!