How to Change In Stock Text in WooCommerce

How to Change In Stock Text in WooCommerce

Do you want to change in stock text in WooCommerce to custom text?

If you want to change the in-stock text in WooCommerce, you need to add this code I will share in this post to your functions file and you will successfully change the in-stock text to your custom text.  Before you learn how to change the in-stock text, you need to learn how this works.

In the previous tutorial on how to change out-of-stock text in WooCommerce, I explained how the filter hook is used to change the text based on the availability of the product in WooCommerce inventory management.

WooCommerce Product in Stock Label

Some WooCommerce themes by default do not display in the stock labels on the product page.

For example, by default when you have them in stock option selected the default WooCommerce theme – Storefront does not show the label ‘in stock’.

If your theme has the same issue and you would like to display the in-stock label on the product page, you can learn how to display out-stock text in WooCommerce from that post.

How to Change In Stock Text in WooCommerce

In most cases when the option to display the in-stock label is selected like in the image above the text should be shown on the product page as seen in the screenshot below:

How to Change In Stock Text in WooCommerce

Now let us focus on the objective of this tutorial to change this text to a custom text.

Steps to Change In-Stock Text in WooCommerce to Custom Text

Suppose you want to change the text to the custom message – ‘Product Available on Request’ The following are the steps that you should take:

  • Log in to your WordPress site to access the dashboard and navigate to the Appearance Menu > Theme Editor. This file can also be downloaded and updated using the FTP access or File Manager on your web hosting CPanel.
  • Open the functions file of your active theme or the child theme since this is where we will place the WooCommerce code snippet to effectively change to in-stock text.
  • Create a filter hook as we did on the tutorial – how to change WooCommerce out-of-stock text. For example, the filter hook can be as follows :
  • Create the callback function that has two parameters, the product, and the availability. This callback function will have the is_in_stock() function that checks if the product is in stock and if it is positive the in-stock text is modified and then returned for display with custom text.
  • Add this code snippet to the functions.php file preferably at the bottom or in the functions.php file of the child theme, save the changes, and check if they are effective.

How to Change Product in Stock Label in WooCommerce

To change the in-stock text you need to add the full code snippet below to your functions.php file and do not forget to edit the text to your preferred text.

/**
 * Code snippet to change WooCommerce In Stock text 
 */ 

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

function change_in_stock_text( $availability, $_product ) {
    
    // Change In Stock Text
    if ( $_product->is_in_stock() ) {
        $availability['availability'] = __('Product Available on Request', 'woocommerce');
    }
   
    return $availability;
}

When you add this code to the functions.php file and you update you should see the change to your custom text as shown below:

How to Change In Stock Text in WooCommerce

 

Display Either Custom Stock and Out of Stock Texts

You can also combine the display of the in-stock and out-stock labels in one function and the code will display either of the text when the condition is met.

In the code below we have combined this code to change in-stock text and the code we added in the previous tutorial – how to change out-of-stock text in WooCommerce. The following is the full code snippet:

/**
 * Code snippet to change WooCommerce In Stock text and Out of Stock Text
 */ 

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

function njengah_custom_stock_text( $availability, $_product ) {
    
    // Change In Stock Text
    if ( $_product->is_in_stock() ) {
        $availability['availability'] = __('Product Available on Request', 'woocommerce');
    }
	
	// Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
        $availability['availability'] = __(' Product Sold Out', 'woocommerce');
    }
   
    return $availability;
}

Conclusion

In this post, we have outlined the way you can change the default WooCommerce in-stock label to your custom text. Like most other WooCommerce product page customizations, you need to learn how to use the filter hooks to change the display of any section to your custom preference.

We have also combined the two code snippets to display custom in-stock and out-of-stock text labels. I hope this tutorial is useful, if you need further guidance, you can feel free to contact me.

Similar Articles

Comments are closed.