25% off ProSNIPC25OFF

Show Amount Saved on Sale Products

Appends a 'You save $X (Y%)' message after the price on WooCommerce sale products, showing both the absolute and percentage discount.

PHPby SnipCraft
php
<?php
if ( ! class_exists( 'WooCommerce' ) ) {
    return;
}

add_filter( 'woocommerce_get_price_html', 'scseed_show_savings_amount', 10, 2 );
function scseed_show_savings_amount( $price_html, $product ) {
    if ( ! $product->is_on_sale() || $product->is_type( 'variable' ) ) {
        return $price_html;
    }
    $regular = (float) $product->get_regular_price();
    $sale    = (float) $product->get_sale_price();
    if ( $regular <= 0 || $sale >= $regular ) {
        return $price_html;
    }
    $saved   = $regular - $sale;
    $percent = round( ( $saved / $regular ) * 100 );
    $price_html .= ' <span class="scseed-you-save">'
        . sprintf(
            /* translators: 1: formatted saved amount, 2: percentage */
            esc_html__( '(You save %1$s &mdash; %2$d%%)', 'scseed' ),
            wp_kses_post( wc_price( $saved ) ),
            $percent
        )
        . '</span>';
    return $price_html;
}
#display#frontend#pricing#sale#woocommerce