Customise WooCommerce out of stock message

In the previous snippet, found here, I showed you how to create a better out of stock approach to hopefully upsell your products.

But perhaps you don’t want to take that approach or you want to accompany it with a customised WooCommerce out of stock message. With this guide you’d be able to change the actual text of the out of stock message and extend it further by using custom fields.

Why would you want to do this? Say you have been stocking a popular item, but before you got more stock in it sold out. Sure, you could utilise the backorder feature, but a lot of people simply don’t read and might miss the fact it’s only available for backorder. If you took a different approach you could customise the message, style it up and even offer a promotion or suggest adding it to a wishlist. The possibilities are endless.

/*
** Snippet change out of stock message
** Author John Cook
** https://wcsuccessacademy.com/?p448
** Tested with WooCommerce 3.3.3
*/
add_filter('woocommerce_get_availability_text', function($text, $product) {
  if (!$product->is_in_stock()) {
    $text = 'Sold out ';
  }
  
  return $text;
}, 10, 2);

You can extend this snippet further by using custom fields to add things such as wishlist buttons, email forms for back in stock notifications or links to stock that is in stock. I’ve included an example below. It’s also worth mentioning that if you happen to forget to enter a custom field it will default to the simple changed text snippet. Also note to leave a space after the text so that the beginning of the new words and the custom field don’t run into one another. In the following example I’m using a custom field called outta_stock, but you can call it whatever you want.

/*
** Snippet change out of stock message and add extra fields using custom fields
** Author John Cook
** https://wcsuccessacademy.com/?p448
** Tested with WooCommerce 3.3.3
*/
add_filter('woocommerce_get_availability_text', function($text, $product) {
  if (!$product->is_in_stock()) {
    $text = 'Sold out ';
  }
  
  return $text . get_post_meta( get_the_ID(), 'outta_stock', true );
}, 10, 2);

Now some themes won’t allow you to change the text, but you can use a neat little feature to translate the selected text into the new text. Avada is one such theme. You can use the below snippet on the Avada theme.

/** 
** Snippet change out of stock message on Avada themes and other themes where you can't change the output text
** Author John Cook
** https://wcsuccessacademy.com/?p448
** Tested with WooCommerce 3.3.3
*/
add_filter('gettext', function($translation, $text, $domain) {
  if ($domain === 'Avada' && $text === 'Out of stock') {
    $translation = 'Sold out';
  }
  
  return $translation . get_post_meta( get_the_ID(), 'outta_stock', true );
}, 10, 3);

Below we can extend the message by using custom fields. As above, you can add forms and buttons plus much more.

/*
** Snippet change out of stock message on Avada themes and other themes where you can't change the output text
** Additionally extend text using custom fields
** Author John Cook
** https://wcsuccessacademy.com/?p448
** Tested with WooCommerce 3.3.3
*/
add_filter('gettext', function($translation, $text, $domain) {
  if ($domain === 'Avada' && $text === 'Out of stock') {
    $translation = 'Sold out';
  }
  
  return $translation;
}

Where do you place the snippets?

Pick one and place it at the end of your child themes functions.php file before the closing ?> (if your theme has it). Alternatively, and as recommended, I suggest that you use a functions plugin. It will allow you to make edits without causing damage should something go wrong and also allow you to use the functions across theme changes without needing to re-add them to the new theme.

Did this work for you?

Leave a comment below if one of the snippets worked for you and any customizations you have made to enhance the code snippets.

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
Scroll to Top