How to Replace Price with Text Call for Price in WooCommerce

How To Woocommerce Replace Price with Text Call for Price

When you are creating a custom WooCommerce theme or site you are required to replace price with a text ‘call for price’.

Some WooCommerce sites require a custom solution for the customer to call to request a quote or request for a price.  Replacing the price on the product with the text to call for the price can sometimes be difficult to implement on your WooCommerce site.

WooCommerce Replace Price with Text Call for Price Plugin

There are free plugins that can help you replace price with text, but most are not well-coded and may create other conflicts with your theme code.

It helps to know how you can add code to replace the price with a text call for the price without using a plugin.

To accomplish this, you should add a filter hook that targets the WooCommerce price section as we outlined in the last two tutorials, – add text before price in WooCommerce and add text after price in WooCommerce.

Replace Price with Text Call for Price

 

Woocommerce Replace Price with Text Call for Price

The following are the steps you should undertake to replace the price on the WooCommerce product with the text – Call for Price.

  1. Log in to your WooCommerce site and open the functions.php file where you will add the code snippet we will create in the next 2 steps.
  2. Create a filter hook that can either hook the following WooCommerce events :
    • woocommerce_get_price_html
    • woocommerce_empty_price_html
  3. Create a callback function and on this function, we are going to use the function return to display the price and the number the customer should call for the price.
  4. When you add this snippet to functions.php save the changes and check if the price has been replaced with the call for price text and the telephone number.

Let us now illustrate the way to add this code to the functions.php file so that you can replace the price with the call for price + the telephone number.

This filter hook can either use the woocommerce_get_price_html or woocommerce_empty_price_html where the first one gets all the prices and the second one targets the price that is empty.

Create Filter Hook to Get WooCommerce Price & Replace All Prices

If your goal is to replace all the prices on your WooCommerce shop with the call for price, you should get all the prices displayed for each of the products, and each of these prices you should replace with the text.

In this case, you should make use of the woocommerce_get_price_html hook as we used it to add text before the price and to add text after the price in WooCommerce WooCommerce price suffix.

WooCommerce Hook: woocommerce_get_price_html

So to replace all the prices on the product with a call for price and the number, you can add the following snippet to functions.php:

/**
* Replace WooCommerce price with text ' Call for Price “and your telephone number
*/

//Hook

add_filter('woocommerce_get_price_html', 'njengah_replace_text_with_call_for_price');

// Callback function

function njengah_replace_text_with_call_for_price() {

   return 'Call for price : +1 800 XXX XXX XXX' ;

}

When you add this code to the functions file you should see the price will be changed to the text as shown in the image below:

Woocommerce Replace Price with Text Call for Price

Create Filter Hook to Get Empty WooCommerce Price & Replace Only the Empty

We can create a filter that checks for all the prices that are left blank and when the price is empty, we will display the text to replace the price with the call for price and add the number as we did in the example above.

WooCommerce Hook: woocommerce_empty_price_html

In this case, we will hook on the WooCommerce event woocommerce_empty_price_html then in the callback function, we return the text to be displayed when the price is left blank or empty.

The following code, when added to the functions.php file, should replace the price with a text call for price only for the empty prices:

/**
 * Replace WooCommerce price with text ' Call for Price " and your telephone number
 */
 
 //Hook 
add_filter('woocommerce_empty_price_html', 'njengah_replace_text_with_call_for_price');


// Callback function 
function njengah_replace_text_with_call_for_price() {
	
     return 'Call for price : +1 800 XXX XXX XXX' ;
	 
}

After you add this code in the functions file and you update the changes, you will see the differences in the display of the text and price as shown in the image below:

Woocommerce Replace Price with Text Call for Price

Final Thoughts

We have outlined the two ways you can replace the price in WooCommerce with the text ‘call for price’. We have essentially highlighted the two WooCommerce hooks that are useful when you are editing the display of price on the product page. I hope you can now add this snippet to your site or WooCommerce theme.

If you have further questions or additional information about this tutorial, you can feel free to get in touch with me. Finally, as an experienced WooCommerce developer, I can help you with your WooCommerce problems; just let me know how I can help you.

Similar Articles

Comments are closed.