DEV Community

Hassam Ul Haq
Hassam Ul Haq

Posted on

Display "FREE" if the price is zero ($0.00) or not set in Woocommerce?

Alt Text

I believe FREE looks good than $0.00

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”.


We really didn’t like showing a $ value of zero and so we replaced it with the text “FREE”. It turns out, that it’s really simple to do in WooCommerce.


Do these changes in child-theme’s function.php
If you have not a child theme then create a backup using Updraft Plugin and then follow the below steps. or you can contact your website theme developer for a Child-theme they can provide you a child-theme.

/**
* @snippet       Display FREE if Price Zero or Empty - WooCommerce Single Product
* @how-to        Display FREE if the price is zero
* @author        Hassam
* @testedwith    WooCommerce 3.8
* @follow        @devhassam
*/
  
add_filter( 'woocommerce_get_price_html', 'wooc_price_free_zero_empty', 9999, 2 );
   
function wooc_price_free_zero_empty( $price, $product ) {
    if ( '' === $product->get_price() || 0 == $product->get_price() ) {
        $price = 'FREE';
    }  
    return $price;
}

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”.

Discussion (0)