Show Zero Price as Free Text in WordPress

You can use a filter in your theme’s file or a custom plugin to display “Free” instead of “0” for a price in WordPress. Here’s an example of how you can achieve this so let’s check out the topic to Show Zero Price as Free Text in WordPress:

  1. Open your theme’s functions.php file or create a custom plugin (recommended).
  2. Add the following code to either the functions.php file or your custom plugin:
add_filter( 'woocommerce_get_price_html', 'show_zero_price_to_free', 9999, 2 );

function show_zero_price_to_free( $price, $product ) {
    if ( $product->is_type( 'variable' ) ) {
        $prices = $product->get_variation_prices( true );
        $min_price = current( $prices['price'] );
        if ( 0 == $min_price ) {
            $max_price = end( $prices['price'] );
            $min_reg_price = current( $prices['regular_price'] );
            $max_reg_price = end( $prices['regular_price'] );
            if ( $min_price !== $max_price ) {
                $price = wc_format_price_range( 'FREE', $max_price );
                $price .= $product->get_price_suffix();
            } elseif ( $product->is_on_sale() && $min_reg_price === $max_reg_price ) {
                $price = wc_format_sale_price( wc_price( $max_reg_price ), 'FREE' );
                $price .= $product->get_price_suffix();
            } else {
                $price = 'FREE';
            }
        }
    } elseif ( 0 == $product->get_price() ) {
        $price = '<span class="woocommerce-Price-amount amount">FREE</span>';
    }
    return $price;
}

Once you’ve added the code and saved the file, the prices with a value of zero should now be displayed as “Free” on your WordPress website.

Leave a Reply