How To Add Custom Out of Stock Text for Specific Products in WooCommerce

How To Add Custom Out of Stock Text for Specific Products in WooCommerce

Every e-commerce store owner knows that an out-of-stock product can be both a blessing and a curse. On one hand, it’s a sign that your products are in demand; on the other hand, it can lead to frustrated customers who are unable to make a purchase. In order to maintain customer satisfaction and reduce cart abandonment, it’s essential to communicate effectively about product availability. One way to achieve this is by adding custom out of stock text for specific products in WooCommerce online store, providing your customers with clear and personalized information about the item they’re interested in—and that’s exactly what we’re going to show you in this guide.

First, we’ll walk you through a simple yet effective method of adding custom out of stock text for specific products in WooCommerce using a custom functions.php code. This approach will enable you to easily tailor your out-of-stock messages for different products without any extensive coding knowledge.

But what if you’re looking for an even more refined and user-friendly solution? We’ve got you covered! We’ve developed a high-performance, optimized custom plugin that provides an elegant interface for managing your out-of-stock text. For just $9, you can take advantage of this powerful tool that not only simplifies the process but also comes with lifetime updates and an incredibly easy-to-use interface. Say goodbye to the hassle of manually editing code and hello to a seamless, professional experience.

Method 1) Custom Code Snippet To Add Custom Out of Stock Text for Specific Products in WooCommerce

Custom Code Snippet To Add Custom Out of Stock Text for Specific Products in WooCommerce
/**

 * Add custom out-of-stock text for specific product IDs

 * 

 * This function allows business owners to add custom out-of-stock

 * text for specified product IDs. It should be added to the

 * theme's functions.php file.

 *

 * @param string $text Default out-of-stock text.

 * @param WC_Product $product Current product object.

 * @return string Custom out-of-stock text for specified product IDs.

 */

function custom_out_of_stock_text( $text, $product ) {

    // Define custom out-of-stock messages for specific product IDs

    $custom_stock_messages = array(

        101 => 'Sorry, this product is currently unavailable.',

        102 => 'We are out of stock. Check back soon!',

        103 => 'This item is sold out for now.',

    );



    // Get the product ID

    $product_id = $product->get_id();



    // Check if the product has a custom out-of-stock message

    if ( array_key_exists( $product_id, $custom_stock_messages ) && !$product->is_in_stock() ) {

        // Return the custom message

        return $custom_stock_messages[ $product_id ];

    }



    // Return the default out-of-stock text

    return $text;

}

add_filter( 'woocommerce_get_availability_text', 'custom_out_of_stock_text', 10, 2 );

This code defines a function called custom_out_of_stock_text, which filters the default out-of-stock text based on product IDs. The $custom_stock_messages array contains the custom out-of-stock messages for specific product IDs. Business owners can simply add or modify the product ID and corresponding custom message in the array to change the out-of-stock text.

The function then checks if the product has a custom out-of-stock message defined in the $custom_stock_messages array and whether the product is out of stock. If the conditions are met, the custom message is returned. Otherwise, the default out-of-stock text is returned.

Finally, the custom_out_of_stock_text function is added to the ‘woocommerce_get_availability_text’ filter with a priority of 10 and 2 arguments.

Explanation of The Code Snippet

In the following sections, we’ll be explaining each function, variable, and filter used in the custom functions.php code that allows business owners to add custom out-of-stock text for specific product IDs.

The custom_out_of_stock_text Function

function custom_out_of_stock_text( $text, $product ) {

    // Define custom out-of-stock messages for specific product IDs

    $custom_stock_messages = array(

        101 => 'Sorry, this product is currently unavailable.',

        102 => 'We are out of stock. Check back soon!',

        103 => 'This item is sold out for now.',

    );



    // Get the product ID

    $product_id = $product->get_id();



    // Check if the product has a custom out-of-stock message

    if ( array_key_exists( $product_id, $custom_stock_messages ) && !$product->is_in_stock() ) {

        // Return the custom message

        return $custom_stock_messages[ $product_id ];

    }



    // Return the default out-of-stock text

    return $text;

}

The custom_out_of_stock_text function is the main function in this code. It takes two parameters: $text and $product. $text is the default out-of-stock text, and $product is the current product object.

The function first defines an array called $custom_stock_messages, which contains the custom out-of-stock messages for specific product IDs. In this example, the product IDs are 101, 102, and 103, and the corresponding messages are “Sorry, this product is currently unavailable.”, “We are out of stock. Check back soon!”, and “This item is sold out for now.” respectively.

Next, the function gets the product ID using the $product->get_id() method. This method returns the product ID of the current product object.

The function then checks if the current product has a custom out-of-stock message defined in the $custom_stock_messages array, and whether the product is out of stock. If both conditions are true, the function returns the custom message. Otherwise, it returns the default out-of-stock text.

The add_filter Function

add_filter( 'woocommerce_get_availability_text', 'custom_out_of_stock_text', 10, 2 );

The add_filter function is used to add the custom_out_of_stock_text function to the ‘woocommerce_get_availability_text’ filter. This filter is used to modify the out-of-stock text displayed on the product page.

The add_filter function takes four parameters:

  • The first parameter is the name of the filter (‘woocommerce_get_availability_text’).
  • The second parameter is the name of the function to be added to the filter (‘custom_out_of_stock_text’).
  • The third parameter is the priority of the filter (in this case, 10).
  • The fourth parameter is the number of arguments that the function accepts (in this case, 2).

Variables Used

$custom_stock_messages = array(

    101 => 'Sorry, this product is currently unavailable.',

    102 => 'We are out of stock. Check back soon!',

    103 => 'This item is sold out for now.',

);

The $custom_stock_messages variable is an array that contains the custom out-of-stock messages for specific product IDs. In this example, the product IDs are 101, 102, and 103, and the corresponding messages are “Sorry, this product is currently unavailable.”, “We are out of stock. Check back soon!”, and “This item is sold out for now.” respectively.

Method 2) Use Our Performance Optimized Plugin To Add Custom Out of Stock Text for Specific Products in WooCommerce

As an online store owner, you understand the importance of providing a tailored experience for your customers. One way to enhance this experience is by customizing the out-of-stock text for specific products in your WooCommerce store. By doing so, you can convey important information to your customers or simply make your store stand out from the competition.

We’ve developed a performance-optimized plugin that allows you to effortlessly add custom out-of-stock text for specific products in WooCommerce. This lightweight plugin is designed to provide a seamless experience without bogging down your site’s performance.

Introducing the Custom Out-of-Stock Text for WooCommerce Plugin

Our plugin, called “Custom Out-of-Stock Text for WooCommerce,” allows you to quickly and easily customize the out-of-stock text for specific products by their product IDs. This means you can tailor your out-of-stock messages for individual products, offering unique information, alternative products, or even a touch of humor to keep your customers engaged.

Key Features of the Custom Out-of-Stock Text for WooCommerce Plugin

  1. Performance Optimized: Our plugin is designed to ensure that it doesn’t negatively impact your site’s performance. It’s lightweight, efficient, and built with performance in mind.
  2. Life-time Update: Buy once and get lifetime update!
  3. Easy to Use: The plugin offers a user-friendly interface, making it simple for store owners to customize the out-of-stock text for specific products. It integrates seamlessly with the WooCommerce settings, providing an intuitive experience.
  4. Translation Ready: The plugin comes with translation support, allowing you to cater to a global audience by translating the out-of-stock messages into different languages.

How to Use the Custom Out-of-Stock Text for WooCommerce Plugin

To start using the Custom Out-of-Stock Text for WooCommerce plugin, follow these simple steps:

  1. Install and activate the plugin on your WordPress website.
  2. Navigate to the WooCommerce settings and locate the newly added “Custom Out-of-Stock Text” tab.
  3. Enter the product IDs for which you want to customize the out-of-stock text, separated by commas.
  4. Specify the custom out-of-stock text you want to display for the specified products.
  5. Save your changes.

That’s it! Now your specified products will display the custom out-of-stock text when they’re not available.

Download The Plugin

Conclusion

The Custom Out-of-Stock Text for WooCommerce plugin is a powerful and performance-optimized solution for enhancing the customer experience in your online store. By offering customized out-of-stock messages for specific products, you can provide additional information or alternatives to your customers, potentially leading to increased sales and customer satisfaction. Give our plugin a try and see how it can improve your WooCommerce store today!

Buffer Pinterest