Display the discounted price and percentage on Woocommerce products

Display the discounted price and percentage on Woocommerce products

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Formatting Discounted Prices and Calculating Savings in WooCommerce

When building a custom pricing display for WooCommerce, simply appending text to the default output often leads to broken layouts or missing formatting. The specific challenge here involves two distinct requirements: ensuring the currency symbol and decimal places are preserved on the sale price, and dynamically calculating the percentage discount. The provided code snippet attempts to address this but fails because it bypasses WooCommerce's built-in localization engine entirely.

The core issue lies in how the woocommerce_get_price_html filter is being used. Your current implementation constructs the HTML string manually using sprintf, passing only the raw numeric difference between regular_price and sale_price. By doing so, you discard the localized currency symbol (e.g., $, €, £) and the standard decimal formatting logic that WooCommerce applies to price fields.

To fix this, you must leverage the existing $price argument passed to the filter hook. This string already contains the correctly formatted regular price with symbols and decimals. You should append your custom discount information to this existing string rather than reconstructing the entire price block. This approach ensures consistency with your theme's currency settings and prevents layout shifts caused by missing characters.

Calculating and Displaying the Percentage

To display the discounted percentage, you need to calculate the ratio between the regular price and the sale price. The formula is straightforward: (Regular Price - Sale Price) / Regular Price * 100. However, implementation details matter here. You must handle edge cases where the regular_price might be null or empty, which would cause a division-by-zero error or a PHP warning.

Additionally, WooCommerce stores prices as strings in many configurations, so you should cast these values to floats before performing mathematical operations to ensure accurate calculations. A robust implementation looks like this:

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

function modify_woocommerce_get_price_html( $price, $product ) {
    // Only apply logic if the product is on sale and we are not in the admin dashboard
    if ( $product->is_on_sale() && ! is_admin() ) {
        $regular_price = floatval( $product->get_regular_price() );
        $sale_price    = floatval( $product->get_sale_price() );

        // Prevent division by zero or invalid calculations
        if ( $regular_price > 0 ) {
            $difference   = $regular_price - $sale_price;
            $percentage   = round( ( $difference / $regular_price ) * 100, 0 );

            // Append the formatted discount percentage to the existing price string
            return $price . sprintf( 
                '&nbsp;<span class="saved-sale">%s%% off</span>', 
                $percentage 
            );
        }
    }

    return $price;
}

This code respects the original $price variable, ensuring that currency symbols and decimals remain intact. It uses get_regular_price() and get_sale_price() methods, which are safer than accessing properties directly, as they handle empty strings more gracefully by returning 0.

Styling Considerations

Once the HTML is generated, the visual presentation depends on your CSS. The class .saved-sale used in the example above should be styled to match your store's design language. You might want to style it as a strikethrough line for the original price (if not already done) and a green or red text color for the discount badge.

Be mindful of the order of operations when modifying woocommerce_get_price_html. Since this filter is applied before the product block renders, any changes you make here will affect all instances where the price is displayed, including cart totals if your theme uses this specific hook for summary calculations. If you find that the total calculation becomes inconsistent because the HTML string includes extra tags or percentage text, you may need to adjust the logic specifically for the cart context using is_cart().

By adhering to these patterns, you maintain compatibility with future WooCommerce updates and ensure that your custom pricing logic integrates seamlessly with the platform's localization system.

Our network