Step 1: Open Theme Editor

Open the WordPress admin area of your WooCommerce store and go to Appearance > Theme Editor.

On the right side, you can see the list of files in your theme. Click on functions.php on the list. Now the functions.php file is opened on the text editor at the center of the page.

Step 2: Copy the following code

Copy the following PHP code snippet.


add_action( 'woocommerce_after_shop_loop_item', 'themelocation_change_outofstock_to_contact_us', 1 );
// for shop page
function themelocation_change_outofstock_to_contact_us(){
global $product;
if(!$product->is_in_stock()){
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart');
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_add_to_cart');
//change the link to your contact us page
echo ' Contact Us ';
}}
// for single page
add_filter( 'woocommerce_get_availability', 'wcs_custom_get_availability', 1, 2);
function wcs_custom_get_availability( $availability, $_product ) {
// Change In Stock Text
if ( $_product->is_in_stock() ) {
$availability['availability'] = __('Available!', 'woocommerce');
}
// Change Out of Stock Text
if ( ! $_product->is_in_stock() ) {
$availability['availability'] = __(' Contact Us ', 'woocommerce');
}
return $availability;
}

 

This snippet will replace the default “Out of stock” label for sold out products with the “Contact us” link to the contact page. You can change the link and link text (Contact us) if you want. I will show you how to change this later.

Step 3: Paste the code snippet to functions.php

If you have clicked on the functions.php file in the first step, you should be seeing functions.php file opened in the text editor.

Paste the above code at the end of the functions.php file.

Step 4: Customize the code

The above code replaced the default “Out of stock” label with “Contact Us”. It’s a link pointing to woocommerce.com

In your case, you want to change the link to the contact page on your WooCommerce website. You can change this by replacing https://woocommerce.com with the link to your website’s contact page.

Similarly, if you want to change the “Contact Us” text to something else, you can change that as well.

Summary

Replacing “Out of stock” text with the contact page link helps you to prevent your customers from finding alternative options. It also helps you to get their contact details so you can notify them when the product is restocked. Fortunately, it is not that difficult to replace the default “Out of stock text” with something else. All you need to do is adding a PHP code snippet we have created to the functions.php file on your website.