Customise WooCommerce out of stock message

This post is more than 7 years old.

Sometimes the WooCommerce out of stock message isn’t quite appropriate. Here’s a couple of ways to change that message.

With wines at a winery, for example, once a product goes out of stock, that’s it — there’s no making more of the 2015 Pinos Gris. Maybe that’s not so bad, but what about that 2014 Hunter Valley Shiraz? 😱 But I digress.

Sometimes it’s better to say that a product is “Sold out”. There’s no implication that it’s coming back any time soon. Customers can’t get it into their heads that they can “hold out” until it does, because the words don’t suggest that it’s just a stocking problem; it’s gone.

Sold out means no refills!

For most WooCommerce websites, you can use a standard WooCommerce filter to change that text.

/**
* wine doesn't go "out of stock" at the winery; it runs out, permanently
* @param string $text
* @param WC_Product $product
* @return string
*/
add_filter('woocommerce_get_availability_text', function($text, $product) {
    if (!$product->is_in_stock()) {
        $text = 'Sold out';
    }

    return $text;
}, 10, 2);

Some themes don’t let WooCommerce set that text, so you need to take a different tack — e.g. Avada hard-codes the “Out of stock” message into its WooCommerce module. Luckily, it allows that text to be translated, which lets us tackle it from another angle — the `gettext` filter.

/**
* Avada: wine doesn't go "out of stock" at the winery; it runs out, permanently
* @param string $translation
* @param string $text
* @param string $domain
* @return string
*/
add_filter('gettext', function($translation, $text, $domain) {
    if ($domain === 'Avada' && $text === 'Out of stock') {
        $translation = 'Sold out';
    }

    return $translation;
}, 10, 3);
It can even be sold out in Avada

Job is done, at least until the Shiraz runs out 🍷 (oh … time for a refill!)