Skip to content

Instantly share code, notes, and snippets.

@kloon
Last active September 8, 2021 17:45
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save kloon/4512943 to your computer and use it in GitHub Desktop.
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 . '%' );
}
?>
@pasog
Copy link

pasog commented May 21, 2018

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

@lacnunga
Copy link

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

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