WooCommerce override left in stock and out of stock text was needed.
In the case of a store that mostly sells unique items placing “1 left in stock” all over the place might not look so good. This was the case for juditmatyus.com .
NOTE: This code may no longer work with the latest version of WooCommerce. Be sure to have backups.
While Woocommerce gives you a few options to play with the word “stock” is always displayed. The main idea here was to get rid of the word “stock” since there is no stock per se, every item is hand made and unique. Once something is sold its gone. Period. There is no stock to refill. There are only a few of each items at any particular time. I was able to change the text to reflect this by doing the following.
1. Change the WooCommerce setting in the dashboard to always show exact number of items.
Go to: WooCommerce > Settings > Products > Inventory and change “Stock display format” to Always show stock e.g. "12 in stock"
2. In your theme’s function.php
file add the following
function juditmatyus_availability( $availability, $product ){
// Change "x in stock" text to "Only x left"
$new_availability_text = 'Only ' . str_replace("in stock", "left", $availability['availability']);
$availability['availability'] = $new_availability_text;
// Change "out of stock" text to "Sold Out"
if( $availability['class'] == 'out-of-stock' ){
$availability['availability'] = 'Sold Out';
}
return $availability;
}
add_filter( 'woocommerce_get_availability', 'juditmatyus_availability', 10, 2 );
I hope this helped. Cheers!
what if I want to change “x in stock” to just “in stock” if i had preselected the option Always show stock e.g. “12 in stock”
Hi ankit,
Thanks for your question,
use the code I posted above in your functions.php but change line 3
From:
$new_availability_text = 'Only ' . str_replace(
"in stock", "left", $availability['availability']);
To:
$new_availability_text = 'in stock';
Feel free to share my blog posts if you find them useful.
Cheers!
hey Arpad,
thanks for the help. I had solved the issue earlier but I am facing a new issue. Kindly see to it and revert back if you could think of an awesome solution. So here it goes:
you must be aware of the product rating system using 5 stars. Now I want to remove that from my product pages. I just want to provide 2 options to my viewers like(+) and unlike(-) instead of stars rating system.
Dear Árpád Lehel Mátyus,
thank you for sharing this hook. I have changed it to this:
function juditmatyus_availability( $availability, $product ){
// Change “x in stock” text to “Only x left”
$new_availability_text = ‘Limitierte Edition ‘ . str_replace(“in stock”, “left”, $availability[‘availability’]);
$availability[‘availability’] = $new_availability_text;
// Change “out of stock” text to “Sold Out”
if( $availability[‘class’] == ‘out-of-stock’ ){
$availability[‘availability’] = ‘Sold Out’;
}
return $availability;
}
add_filter( ‘woocommerce_get_availability’, ‘juditmatyus_availability’, 10, 2 );
I would love that it would say Limited Edition x erhaeltlich. If I change – then only the new text appears, neither Limited Edition nor the quantity. Cheers from Germany from Ina
Hi Ina,
I) Before we try to debug it, can you please confirm that you did step 1? To set woocommerce always show exact number? We need to make sure its printing “12 in stock”
1. Change the WooCommerce setting in the dashboard to always show exact number of items.
Go to: WooCommerce > Settings > Products > Inventory and change “Stock display format” to Always show stock e.g. “12 in stock”
II) Then use string replace to modify it, replace “in stock” with “erhaeltlich”
III) Please specify exactly what is showing if you try the code.
Please double check these and if it didn’t work we can come back and try to debug the issue further.
Viel Glück for Germany in UEFA Euro 2016! I am rooting for Hungary and Germany. 🙂
Thanks,
Lehel
Dear Lehel, Pinocchios nose is what it is – a piece of wood that won’t grow anymore once it’s cut from its roots *~* Germany needs luck in many ways . . . and I am – the child to be – forever. I like your blog *~*
Coming back to my original question. I did everything as you described. The only thing that doesn’t happen is – the word “vorrätig” won’t change! But please don’t invest to much time in this. I just try things with trial and error – sometimes I am lucky and sometimes it just doesn’t want to come together. That brings me to the new shipping in WooCommerce 2.6.1 – huiiiii – till now I don’t get it at all. And no legacy to fall back on to – because I just started the pluging on this website with the latest version.
Please tell Judit her artwork is beautiful. All the best from Ina
Hello Lehel, found this hook combined with yours it does what it is supposed to: Limitierte Edition x erhaeltlich. Can you combine these two hooks *~* that would be great . . .
add_filter( ‘woocommerce_get_availability’, ‘custom_get_availability’, 1, 2);
function custom_get_availability( $availability, $_product ) {
global $product;
$stock = $product->get_total_stock();
if ( $_product->is_in_stock() ) $availability[‘availability’] = __($stock . ‘ erhältlich’, ‘woocommerce’);
if ( !$_product->is_in_stock() ) $availability[‘availability’] = __(‘SOLD OUT’, ‘woocommerce’);
return $availability;
}
Hi Ina,
This looks like a bit more elegant solution than mine, I was trying not to invoke a global variable but this looks good to me.
If this hook works for you then all you need to do is modify the text a little.
Here is the full modified version
https://gist.github.com/lehelmatyus/2a3003a96025e613317f62d0db259630
Let me know if this works, I am curious.
Judit says “tank you, and Hi”! Also if you are ever interested in her Jewelry I would suggest signing up for her biweekly newsletter first, she has a lot of promotions going on all the time.
http://www.juditmatyus.com/newsletter-signup/
Follow up on the Woocommerce hook, I am curious if you were able to fix it.
Cheers!
Lehel
Actually – dear Lehel – your hook worked just fine. The other version added all stock to one sum in variation availability (when you have a product with variations). My mistake was I changed “left” into “erhaeltlich” – but I didn’t change “in stock” to “vorraetig”. I subscribe to J’s newletter but got an error message “Oops. Something went wrong. Please try again later.” I will try again. Hope our pathes will cross again. Coffee will be on me . . . *~*
Dear Ina,
I am really glad the code worked. Great to hear I was able to help.
Thanks for finding the bug on the JM website signup form, it looks like Mailchimp changed their API and made Last Name field mandatory.
I had to add the extra field to the form:
http://www.juditmatyus.com/newsletter-signup/
Looking forward to that coffee. 🙂
Lehel
Dear Lehel, there is one more question “is it possible to change the text based on product category”? Lets say I offer courses I would write “Noch x Plaetze frei” but in case I sell Vouchers it should read “x erhaeltlich” and for prints “Limited Edition x verfuegbar. What do you think? And where shall we have coffee, in the US or in Europe *~*
Cheers from me . . .
Hi Arpad,
Thanks for your time and effort in helping others here! I have a slightly different situation and I’d be interested to see if you have any idea on how to solve it. I also sell only handmade unqiue items. When an item is sold, it is sold. The tweak I’d like to make is that when an item is sold, instead of saying “Out of stock”, it should say “Available on request”.
Do you have any idea how I would enact this change? I would prefer not to have every item in my store say “1 in stock”.
Thanks in advance for any advice you may have!
Cheers
Brad
Hi Brad!
Try this in your theme’s function.php , always back up the file before making changes. or use git, I have a video on it.
Let me know how it works out.
Cheers!
Lehel
Hi Arpad,
I am so sorry for the really really late reply on this. I’ve had a family crisis that had demanded full attention. I really appreciate you going to the trouble of trying to code a solution for me. Unfortunately it didn’t work. “Out of stock” still displays even after I added your code to the functions.php file.
Thanks again for your kindness in attempting to help.
Best
Brad
Hi Brad,
Sorry to hear about the family crisis. I wish you endurance through tough times.
The original code may be outdated because of lots of upgrades WooCommerce did. So I came up with a quick and dirty CSS hack that might help you. Add it to your child theme’s CSS.
Here is the link to the Gist
https://gist.github.com/lehelmatyus/f2687f89c8c4ba7a266f193708443aef
Let me know if this worked.
Best
Lehel
Hi Árpád,
Thank you som much for your code.
I tried changing the “in stock” text with “get_total_stock” but that obviously lead to problems with product variations.
You code solved this issue all at once.
Thank you so much!🙏
Cheers,
Tom
Hi,
Any idea how need to look the code to replace:
11 in stock with
Only 11 left in stock. ?
Thank you,
Hi Virgil,
The code is just doing string replace so you should be able to do it.
Try this:
function virgil_availability( $availability, $product ){
// Change “x in stock” text to “Only x left in stock”
$new_availability_text = ‘Only ‘ . str_replace(“in stock”, “left in stock.”, $availability[‘availability’]);
$availability[‘availability’] = $new_availability_text;
return $availability;
}
add_filter( ‘woocommerce_get_availability’, ‘virgil_availability’, 10, 2 );
Cheers,
Lehel