Do you need to replace the default WooCommerce pricing label for a free product? Perhaps you offer some, if not all, of your products at no cost. Instead of showing the default WooCommerce price label of $0.00, you want to show custom text, such as the word “FREE” or “Download Now”.
Use our plugin to change the price label to “Free”
This is a code snippet and requires knowledge of PHP to get it working. If you’re not wanting to use code to show “Free” instead of $0 and would prefer a plugin solution instead, then checkout the Hide Prices plugin built by us. It allows you to hide prices and the add to cart button, but also replace the price with a simple text label. This text label can be used to display “Free” instead of $0.
On the WP Geeks website, we’ve done exactly that. A number of our WooCommerce plugins that are listed on our site are completely free to download and use. We really didn’t like showing a dollar value of zero and so we replaced it with the text “FREE”. It turns out, that it’s really simple to do in WooCommerce.
What is required is a quick filter to override the price of the product and replace it with the text of your choice.
Change A Free Product’s WooCommerce Price Label
Just copy and paste the following snippet in to your functions.php file and edit the label text as indicated in the snippet’s comment below.
<?php
/**
* Display "FREE" instead of $0 if the item is free.
*
* @param string $price The current price label.
* @param object $product The product object.
* @return string
*/
function wpgeeks_price_override( $price, $product ) {
if ( empty( $product->get_price() ) ) {
/*
* Replace the word "Free" with whatever text you would like. Also
* remember to update the textdomain for translation if required.
*/
$price = __( 'FREE', 'textdomain' );
}
return $price;
}
add_filter( 'woocommerce_get_price_html', 'wpgeeks_price_override', 100, 2 );
Breaking it down, we’re just checking the price of the product to make sure that it is “empty” i.e. free, and then if it is, returning different label text which in the case above is the word “FREE”.
If the product is not free, then we don’t change anything and return the original price.
It’s as easy as that. Do let us know if you run in to any issues and we will try our best to help you out.
Leave a Reply