Show "Free" Instead of a Zero Price
Displays the word "Free" for products priced at 0 instead of showing $0.00.
Opens WPIDE on
Code
add_filter( 'woocommerce_get_price_html', function ( $html, $product ) {
$label = 'Free'; // Shown instead of a zero price.
if ( ! $product instanceof WC_Product || $product->is_type( array( 'variable', 'grouped' ) ) ) {
return $html;
}
if ( '' !== $product->get_price() && 0.0 === (float) $product->get_price() ) {
return '<span class="amount wpide-free-price">' . esc_html( $label ) . '</span>';
}
return $html;
}, 10, 2 );
Notes
Change $label to any wording you like, for example "Free download".
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.