Show saving on WooCommerce sales items

cegoh / Pixabay

WooCommerce will automatically add sales CSS to items on sale and show you the regular price, however it doesn’t show you the total saved.
Here’s how to show the saving.

Home   Advanced Muscle Design

Add the following to your theme’s functions.php.

add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 );
function woocommerce_custom_sales_price( $price, $product ) {
 
	$percentage = round( $product->regular_price - $product->sale_price  ,2 );
 
	return $price . sprintf( __(' <br /><span class="price_save">SAVE £%s', 'woocommerce' ), $percentage . '</span>' );
 
}
 
add_filter( 'woocommerce_variable_sale_price_html', 'woocommerce_custom_variable_sales_price', 10, 2 );
function woocommerce_custom_variable_sales_price( $price, $variable ) {
 
	$reg_price = get_post_meta( $variable->children[0], '_regular_price', true );
	$sale_price = get_post_meta( $variable->children[0], '_sale_price', true );
 
	$percentage = round( $reg_price - $sale_price  ,2 );
 
	return $price . sprintf( __(' <br /><span class="price_save">SAVE £%s', 'woocommerce' ), $percentage . '</span>' );
 
}

Author: Kieran Barnes

Kieran is a PHP developer with 15 years commercial experience. Specialist in WordPress, CakePHP, CubeCart and all things PHP.

6 thoughts on “Show saving on WooCommerce sales items”

  1. Hi Kieran, thanks for this post. It’s exactly what I’ve been looking for! I tried to use it with percentages, but it keeps giving me an error on line 18. Do you have any idea what might cause it? The error is “Warning: Division error on line 17”

    regular_price – $product->sale_price ) / $product->regular_price ) * 100 );

    return $price . sprintf( __(‘ save %s’, ‘woocommerce’ ), $percentage . ‘%’ );

    }

    add_filter( ‘woocommerce_variable_sale_price_html’, ‘woocommerce_custom_variable_sales_price’, 10, 2 );
    function woocommerce_custom_variable_sales_price( $price, $variable ) {

    $reg_price = get_post_meta( $variable->children[0], ‘_regular_price’, true );
    $sale_price = get_post_meta( $variable->children[0], ‘_sale_price’, true );

    $percentage = round( (($reg_price – $sale_price) / $reg_price ) * 100 );

    return $price . sprintf( __(‘ save %s’, ‘woocommerce’ ), $percentage . ‘%’ );

    }

    ?>

  2. OMG Finally! Thanks alot!
    Everywhere i found this code it didnt work (because they only cased SINGLE PRODUCTS), but this has Variable products as well.. awesome.. thanks alot 🙂

  3. Jay – this is what you are looking for.

    // SINGLE PRODUCT
    add_filter( ‘woocommerce_sale_price_html’, ‘woocommerce_custom_sales_price’, 10, 2 );
    function woocommerce_custom_sales_price( $price, $product ) {

    $percentage = round( $product->regular_price – $product->sale_price ,2 );

    return $price . sprintf( __(‘ Spar %s %’, ‘woocommerce’ ), $percentage . ” );

    }

    // VARIABLE PRODUCT
    add_filter( ‘woocommerce_variable_sale_price_html’, ‘woocommerce_custom_variable_sales_price’, 10, 2 );

    function woocommerce_custom_variable_sales_price( $price, $variable ) {

    $reg_price = get_post_meta( $variable->children[0], ‘_regular_price’, true );
    $sale_price = get_post_meta( $variable->children[0], ‘_sale_price’, true );

    //$percentage = round( $reg_price – $sale_price ,2 );
    $percentage = round( ( ( $reg_price – $sale_price ) / $reg_price ) * 100 );

    return $price . sprintf( __(‘ Spar %s %’, ‘woocommerce’ ), $percentage . ” );

    }

  4. Hi! Thanks for code, it works. But what about both price & percentage economy showing together?

Leave a Reply

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