How to Display Stock Availability Text in WooCommerce

Display the In stock Text in WooCommerce

Do you want to display the stock text in the WooCommerce theme that does not show this text by default?

In stock or stock availability, the text is not shown by default on the Storefront theme and you may want to show this text.

In this quick tutorial, I will guide you on how to show in-stock text in the WooCommerce theme.

First, you need to understand how inventory management works in WooCommerce. When you are publishing a product in WooCommerce it is possible to set the stock status as we highlighted in the previous two tutorials – change out-of-stock text and change the in-stock text in WooCommerce.

WooCommerce Stock Availability Text

The stock availability text in WooCommerce can be out of stock, on backorder, or in stock status. These statuses can be set for each WooCommerce product under the inventory tab as shown on the image below:

stock status

If this is set and you still do not see the WooCommerce stock availability text, you need to add a filter to your theme functions.php file to display the missing stock availability text.

This hook filters the content that is added to the functions.php and then displays the content after we add the text we want to display as the stock availability text.

By default, the WooCommerce storefront theme does not display in-stock text and you may have to add a code snippet to display this text.

How to Display In-Stock Text in WooCommerce or Out Stock

The best approach to display stock availability text is to combine both the in-stock and out-stock text in one filter and display them conditionally. The following are the steps:

  1. Login to your WordPress site to access the dashboard
  2. Open the theme editor under Appearance Menu > Theme Editor
  3. Open the functions.php file where we will add the code snippet.
  4. Create the WooCommerce filter hook that will hook on the ‘woocommerce_get_availablity’ example : add_filter(‘‘woocommerce_get_availablity’, ‘njengah_woocommerce_stock_availablity_display’)
  5. Create a callback function where you should have the conditional check on the status of the stock using the is_in_stock() function.

Now lets us illustrate these steps with an example

Display the In stock Text in WooCommerce

The following code should be added to the functions.php  to add the WooCommerce stock availability text. When you add this code you can test the stock status by changing on one of the products:

/**
* WooCommerce Display Stock Availablity 
*/
 
add_filter( 'woocommerce_get_availability', 'njengah_display_stock_availability', 1, 2);

function njengah_display_stock_availability( $availability, $_product ) {
	
   global $product;
 
   // Change In Stock Text
    if ( $_product->is_in_stock() ) {
        $availability['availability'] = __('Product is Available!', 'woocommerce');
    }
 
    // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
    	$availability['availability'] = __('Product sold out!', 'woocommerce');
    }
 
    return $availability;
}

How the Code Works

This callback function has two parameters that are passed (product and availability). These two parameters are used to check the status of the product in the function – is_in_stock().

In the first block of code we are checking if the product we passed to the function is available and if this is the case we display in stock text:

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

display

 

In the second code block we are checking if the product we passed to the function is out of stock if you check keenly we are using the! / Not symbol to get the reverse of the first condition and when this evaluates to true the product is out of stock and we display the out-of-stock text:

   // Change Out of Stock Text
    if ( ! $_product->is_in_stock() ) {
    	$availability['availability'] = __('Product sold out!', 'woocommerce');
    }

change Out Of Stock Text

Finally, in the function’s return, we have to return the modified availability so that we can display the appropriate message.  You can edit WooCommerce stock availability text to a custom text for either case by changing the respective text in each of the code blocks above.

Conclusion

In this tutorial, we have outlined how you can display WooCommerce stock availability text and in particular how you can display both the in-stock and out-of-stock text in one function.  This code can be edited to have custom text that is tailored to your specific needs as I explained in these two tutorials – how to change WooCommerce in stock text and how to change out-of-stock text in WooCommerce.

Similar Articles

 

Comments are closed.