Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active April 25, 2024 16:58
Show Gist options
  • Select an option

  • Save kloon/4512943 to your computer and use it in GitHub Desktop.

Select an option

Save kloon/4512943 to your computer and use it in GitHub Desktop.
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 . '%' );
}
?>
@boutzamat

Copy link
Copy Markdown

This snippet doesnt work on the latest version..

@mac4media

Copy link
Copy Markdown

@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

Copy link
Copy Markdown

@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-

Copy link
Copy Markdown

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

@prashant9912

Copy link
Copy Markdown

edit and delete that code from server file manager

@tozbey

tozbey commented Apr 7, 2017

Copy link
Copy Markdown

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

@ifti251

ifti251 commented Apr 17, 2017

Copy link
Copy Markdown

not working on a variable products

@giroin

giroin commented Apr 25, 2017

Copy link
Copy Markdown

Any update for woocommerce 3? thank you!

@AntoT84

AntoT84 commented May 26, 2017

Copy link
Copy Markdown

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

giroin commented Jun 3, 2017

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

bladex21 commented Jun 19, 2017

Copy link
Copy Markdown

Does anyone know of a code that works with variations ?

@matthewdean89

Copy link
Copy Markdown

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

Copy link
Copy Markdown

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

@zdvdla

zdvdla commented Sep 4, 2017

Copy link
Copy Markdown

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

zdvdla commented Sep 4, 2017

Copy link
Copy Markdown

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

pasog commented May 21, 2018

Copy link
Copy Markdown

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

@lacnunga

Copy link
Copy Markdown

This code apparently doesn't work in my instance of WooCommerce Version 5.6.0, although it's been copied into the functions.php of the Storefront theme as is. Any suggestions for troubleshooting would be appreciated!

@vandanojan

Copy link
Copy Markdown

Your snippet only works on simple products. For variable products, instead, give the following code a try:

// For Variable Products Discount
add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 );
function custom_variation_price_saving_percentage( $data, $product, $variation ) {
$active_price = $data['display_price'];
$regular_price = $data['display_regular_price'];

if( $active_price !== $regular_price ) {
$saving_percentage = round( 100 - ( $active_price / $regular_price * 100 ), 1 ) . '%';
$data['price_html'] .= sprintf( __('You Saved: %s', 'woocommerce' ), $saving_percentage );
}
return $data;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment