Rename Price of Free Products

Displays "Free" for products with a price of zero or empty.

PHP
/**
 * Snippet Name:     Rename price of free products
 * Snippet Author:   coding-bunny.com
 * Description:      Displays "Free" for products with a price of zero or empty.
 */

add_filter( 'woocommerce_get_price_html', 'cb_rename_free_product_price', 10, 2 );

/**
 * Rename the price display for products that are free.
 *
 * @param string $price   The price HTML.
 * @param WC_Product $product The product object.
 * @return string Modified price HTML.
 */
function cb_rename_free_product_price( string $price, WC_Product $product ): string {
    // Check if the product price is empty or zero
    if ( empty( $product->get_price() ) || $product->get_price() == 0 ) {
        // Set the price display to "Free"
        $price = __( 'Free', 'woocommerce' );
    }
    return $price;
}

How To Implement This Solution?

Leave a Reply

Your email address will not be published. Required fields are marked *

My Agile Privacy
This site uses technical and profiling cookies. You can accept, decline or customize cookies by pressing the desired buttons. By closing this policy you will continue without accepting.

Need help?

Choose one of the following options:

Powered by CodingBunny