How to Change Out Of Stock Text in WooCommerce

WooCommerce change out of stock text

Are you looking for a simple way to change out-of-stock text in WooCommerce for your online store?

If you do, changing this WooCommerce out of stock text should not be a nightmare as you will shortly find out in this post.

You can easily add your custom text to replace the out-of-text WooCommerce default text using a simple code snippet placed in your theme functions.php file.

If you would prefer to hide out-of-stock products in your WooCommerce store, I explained here the two ways you can hide all products that are out of stock in WooCommerce.

WooCommerce out of Stock Inventory Management

The out-of-stock text in WooCommerce is an important inventory indicator of the status of the stock for a certain product.

If you are new to WooCommerce, you need to learn how inventory management works in WooCommerce.

Ideally, inventory management in WooCommerce online shop works just like an offline brick and mortar business where you have available stock and out of stock products as well as backorder stock status.

All these stock statuses can be set for a specific product on the publishing screen under inventory and on the stock status option as shown below:

How to Change Out Of Stock Text in Woocommerce

When you select the out-of-stock and update the product the out-of-stock message is displayed on the front side as shown below:

How to Change Out Of Stock Text in Woocommerce

For illustration, I am using the default WooCommerce theme – Storefront that I have slightly customized. The display of the out-of-stock text in WooCommerce themes is not a default setting and may vary from one WooCommerce theme to another.

Steps to Change WooCommerce Out of Stock Text to Custom Text

The following are the steps you should undertake to change the WooCommerce out-of-stock text to your own custom text:

  1. Log in to your WordPress dashboard and under the Appearance dashboard menu open the Theme Editor to access the active theme. You can alternatively use the FTP client or the web hosting CPanel to access the active theme file.
  2. Open the functions.php file of the active theme and preferably use the child theme if the active theme has a child theme.
  3. Create a filter hook that hooks on the woocommerce_get_availability and has a callback function that will check if the product is in stock and if it is not in stock the message ‘ out of stock’ will be filtered and returned as your custom text. For example;add_filter( 'woocommerce_get_availability', 'change_out_of_stock_text_woocommerce', 1, 2);
  4. Add now the callback function and ensure you pass the two parameters (product and availability) to check availability using the is_in_stock() WooCommerce function and return your modified custom text.
  5. Save the changes and check the product page if the changes are effective.

Change WooCommerce Out of Stock Text

Let us illustrate using an example the steps we have highlighted above so that we can test the code snippet and see if we successfully change out of stock text to our custom message:

Filter Hook

As usual, when changing the text on the WooCommerce product page you need a filter hook that targets specific places on the WooCommerce loading sequence. The filter hook should be as follows:

add_filter( 'woocommerce_get_availability', 'change_out_of_stock_text_woocommerce', 1, 2);

Callback Function

The callback function as we stated above needs two parameters and we need to use the is_in_stock function to check if the product is in stock before we can display the customized message:

function change_out_of_stock_text_woocommerce( $availability, $product_to_check ) {
    
    // Change Out of Stock Text
    if ( ! $product_to_check->is_in_stock() ) {
		
        $availability['availability'] = __('Product Sold Out', 'woocommerce');
		
    }
    return $availability;
}

Code Snippet to Change Out of Stock Text

This is the full code snippet that you should add to the functions.php to change the out of stock text to your custom text as I have illustrated.

On the line that has the words ‘Product Sold Out,’  you should replace that text with your preferred text and save the changes:

/**
 * Code snippet to change WooCommerce Out of Stock text 
 */ 

add_filter( 'woocommerce_get_availability', 'change_out_of_stock_text_woocommerce', 1, 2);


function change_out_of_stock_text_woocommerce( $availability, $product_to_check ) {
    
    // Change Out of Stock Text
    if ( ! $product_to_check->is_in_stock() ) {
		
        $availability['availability'] = __('Product Sold Out', 'woocommerce');
		
    }
    return $availability;
}

When you add the code snippet and you update your theme files you should see the change to the new text as shown below :

WooCommerce Change Out Of Stock Text

Conclusion

In this post, we have outlined the steps you should take to change the WooCommerce out-of-stock message, tested it practically, and also shared the code snippet.

This is a simple and rather straightforward process and can be implemented nearly on all WooCommerce themes.

If you are not sure about how to get this code working on your website, you can get in touch with me for more help and guidance.

Similar Articles

Comments are closed.