DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’» is a community of 966,904 amazing developers

We're a place where coders share, stay up-to-date and grow their careers.

Create account Log in
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”.

Top comments (0)

🌚 Friends don't let friends browse without dark mode.

Sorry, it's true.