Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
WooCommerce add "Save x%" next to sale prices
<?php
// Add save percent next to sale item prices.
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 ) / $product->regular_price ) * 100 );
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' );
}
?>
@rsangeethk

This comment has been minimized.

Copy link

@rsangeethk rsangeethk commented Sep 5, 2013

thanks alot it helped me,do i need to add any code in theme to call the save :percentage

@kloon

This comment has been minimized.

Copy link
Owner Author

@kloon kloon commented Sep 8, 2013

@rsangeethk No, you simply add the code to your theme's functions.php file and it will automatically add the Save x% next to prices of products that are on sale.

@ShimonGrout

This comment has been minimized.

Copy link

@ShimonGrout ShimonGrout commented Jun 25, 2014

@kloon. Thanks a lot.
I'm trying to add a class to that percentage so i can manage the CSS. Do you have an idea to do that please?

@oavs

This comment has been minimized.

Copy link

@oavs oavs commented Jul 16, 2014

Above works fantastically. Thanks for that.

Can you also convert the above code for
// Add save percent next to variable sale item prices?

I have following code but it gives [Warning; Divide by zero] only when the products are listed in the side bar. You can see it here http://www.silkaurora.com.au/

// Add save percent next to variable sale item prices. This is causing 'devide by zero' warning on the left hand widget area
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 . '%' );
}

@aureliendebord

This comment has been minimized.

Copy link

@aureliendebord aureliendebord commented May 20, 2015

@ShimonGrout For the css styling, I propose this :

// Add save percent next to sale item prices.
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 ) / $product->regular_price ) * 100 );
return $price . sprintf( __('

Save %s', 'woocommerce' ), $percentage . '%
' );
}

@intikhabalam123

This comment has been minimized.

Copy link

@intikhabalam123 intikhabalam123 commented Jun 17, 2015

@kloon i have just copy and paste the above code mention for functions.php at the bottom of my functions.php but its getting error.. Please help me how can i make the percentage on the product of sale price.

Thank in advance !!

@boutzamat

This comment has been minimized.

Copy link

@boutzamat boutzamat commented Jun 30, 2015

This snippet doesnt work on the latest version..

@mac4media

This comment has been minimized.

Copy link

@mac4media mac4media commented Sep 14, 2015

@kloon works perfectly thanks, i'm also trying to add a css class to manage which prices display the saving.
any idea how to add the class to this snippet? many thanks!

@abhisekpadhi

This comment has been minimized.

Copy link

@abhisekpadhi abhisekpadhi commented Nov 12, 2015

@kloon , how can I output the total savings in the cart page. How to modify the foreach loop to calculate total savings of all products the cart and show it in the cart page, what filter/hook to use ?

This code works perfectly for single product page.

Can you please help me modifying the function to show total savings of all the products in the cart, in the cart page.

Thanks.

@johncarter-

This comment has been minimized.

Copy link

@johncarter- johncarter- commented Nov 24, 2015

@oavs Did you manage this to work on variable products?

@prashant9912

This comment has been minimized.

Copy link

@prashant9912 prashant9912 commented Jun 7, 2016

edit and delete that code from server file manager

@tozbey

This comment has been minimized.

Copy link

@tozbey tozbey commented Apr 7, 2017

This isn't working with the latest WooCommerce update.. please can you update page? thank for you!

@ifti251

This comment has been minimized.

Copy link

@ifti251 ifti251 commented Apr 17, 2017

not working on a variable products

@giroin

This comment has been minimized.

Copy link

@giroin giroin commented Apr 25, 2017

Any update for woocommerce 3? thank you!

@AntoT84

This comment has been minimized.

Copy link

@AntoT84 AntoT84 commented May 26, 2017

Try this:

function woocommerce_saved_sales_price( $price, $product ) {
$saved = wc_price( $product->regular_price - $product->sale_price );
return $price . sprintf( __('Saved %s', 'woocommerce' ), $saved );
}
add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 );

@giroin

This comment has been minimized.

Copy link

@giroin giroin commented Jun 3, 2017

For those who want a percentage just try this

function woocommerce_saved_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __('-%s', 'woocommerce' ), $percentage . '%' );
}
add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 );

@ketanzluck

This comment has been minimized.

Copy link

@ketanzluck ketanzluck commented Jun 16, 2017

Hi giroin,

I have used following your code to show total percentage discount on Product page and categories page:

function woocommerce_saved_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __('-%s', 'woocommerce' ), $percentage . '%' );
}
add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 );

It's work good. But This code also show Total percentage off on without Sale Products . See this screen shot -> http://nimb.ws/y4xk5X So please provide me solutions for how to hide this on without sale price.

I am waiting for your replay.

Thanks,
Ketan.

@bladex21

This comment has been minimized.

Copy link

@bladex21 bladex21 commented Jun 19, 2017

Does anyone know of a code that works with variations ?

@matthewdean89

This comment has been minimized.

Copy link

@matthewdean89 matthewdean89 commented Jul 10, 2017

try this:

`function woocommerce_saved_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
if ($percentage == "100") {
return $price . sprintf();
} else {
return $price . sprintf( __('-%s', 'woocommerce' ), $percentage . '%' );
}
}

add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 );`

@MuhammedRaza

This comment has been minimized.

Copy link

@MuhammedRaza MuhammedRaza commented Aug 15, 2017

Its not working on variable products.Please help me out.
Its Urgent!
Thanks in advance.

@zdvdla

This comment has been minimized.

Copy link

@zdvdla zdvdla commented Sep 4, 2017

the code above does not work with WC 3.1.2....
It doesn't throw any errors, simply does nothing....
I'll work on it but was thinking I couldn't possibly be the only one facing this...
any help would be great!
I'll see what I can come up with in the meantime.
Thanks!

@zdvdla

This comment has been minimized.

Copy link

@zdvdla zdvdla commented Sep 4, 2017

found a solution here-
https://stackoverflow.com/questions/43757920/display-the-discounted-percentage-near-sale-price-in-single-product-pages-for-wc

answered May 3 at 22:46
LoicTheAztec

Thank you Loic !!
I looked for 2 hours and you saved me e few more I am certain!!

@pasog

This comment has been minimized.

Copy link

@pasog pasog commented May 21, 2018

is it possible to do this even for different prices, variable products? !!
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.