WooCommerce Stock availability text

In this tutorial I will show you how to change the WooCommerce Stock availability text and explain how it works. You will be able to change the text of “In Stock”, “Only ‘some quantity’ left” & “Out of stock” by following this simple how to guide.

Theme’s Function.php

Copy and Past this code below into your theme’s function (/root/wp-content/themes/YOURTHEME/function.php) file at the bottom, just before the closing ?> sign.

PS. The closing  “?> sign is actually telling PHP Server to stop reading the PHP language when you hit that closing tag. So you want to make sure your code is before that tag else it will not be read and WordPress may crash with a error:

The code:

You can ignore the code for now. The areas that you need to focus on is between the round brackets, in-between the ‘single quotes’.

These are called text “strings” in the coding world. You can replace these with your own text and just save the file and refresh you browser. Very important! Don’t remove the single quotes else the code will break and your page will not load.

Change in Stock Text

In stock availability text

As explained above you can just change the following.:

 

Example.:

$availability[‘availability’] = __(‘In Stock!‘, ‘woocommerce’);

to

$availability[‘availability’] = __(‘Whatever you want to say‘, ‘woocommerce’);

 

Change in Stock Text to only 1 or 2 left

quantity stock availability text

The second part which shows what quantity is left…

…can be adjusted for any other quantity for example.:

Now it will show.: Only 4 left in store! The <= comparison sign shows that it must only show smaller or equal to 4 products left.

The code in this part is also a bit different than the rest. It is because we need to fetch the amount of stock left and place it in the string (text area). You will notice the %s in.: Only %s left in store!

This %s is a “place holder” for a variable. In this case we call the variable from the the global object $product.: $product->get_stock_quantity(); This is only for a explanation. If you don’t understand it, don’t worry about it.

 

Example.:

$availability[‘availability’] = sprintf( __(‘Only %s left!’, ‘woocommerce’), $product->get_stock_quantity());

To

$availability[‘availability’] = sprintf( __(‘Only %s left in store!‘, ‘woocommerce’), $product->get_stock_quantity());

Change Out of Stock Text

Nothing fancy here. Just perform the same steps as “Change in Stock text”.

 

Example.:

$availability[‘availability’] = __(‘Out of Stock!‘, ‘woocommerce’);

To

$availability[‘availability’] = __(‘Sorry, All sold out!‘, ‘woocommerce’);

 

Hope you enjoyed this quick tutorial on how to change the WooCommerce Stock availability text.