Contents
If you want to change the price of 0 or leave the price field empty to display “Call for price” in WooCommerce, you can refer to the code below.
Code to Change 0 Price or Empty Price to “Call for Price” in WooCommerce
You can add the following code to the functions.php
file of your child theme:
add_filter('woocommerce_get_price_html', 'custom_empty_price_message', 10, 2);
function custom_empty_price_message($price, $product) {
if ($product->get_price() === '' || $product->get_price() == 0) {
$price = '<span class="webantam-woocommerce-price-contact">Call for Price</span>';
}
return $price;
}
Code Explanation:
- if ($product->get_price() === ” || $product->get_price() == 0): This conditional statement checks whether the product price is either an empty string (meaning you didn’t enter a price) or if the price is 0.
- $price = ‘Call for Price’; If the above condition is met, this line replaces the product price with the HTML string
<span class="webantam-woocommerce-price-contact">Call for Price</span>
. This will display the text “Call for Price” on the WooCommerce product page.
Conclusion:
With just a simple code snippet, you can easily solve the issue of changing a 0 price or empty price to display “Call for Price” in WooCommerce. If you encounter any issues during the process, feel free to reach out!