Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save mommaroodles/bcd608163c92d79e3828536645faf615 to your computer and use it in GitHub Desktop.
Save mommaroodles/bcd608163c92d79e3828536645faf615 to your computer and use it in GitHub Desktop.
WooCommerce: Saved on Discount Amount and Percentage for Simple Products
add_filter( 'woocommerce_get_price_html', 'bdev_display_sale_price_and_percentage_html', 10, 2 );
function bdev_display_sale_price_and_percentage_html( $price, $product ) {
// sale products on frontend excluding variable products
if( $product->is_on_sale() && ! is_admin() && $product->is_type('simple')) {
// product prices
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_price();
if( $regular_price > 0 && $sale_price > 0 ) {
// price calculation and formatting
$saving_price = wc_price( $regular_price - $sale_price );
// percentage calculation and formatting
$precision = 1;
$saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 0 ) . '%'; // change 0 into how many decimals you want to show
// display the formatted html price including amount and precentage using a span tag which means displaying it on the same row, if you want this on a new row, change the tag into a paragraph
$price .= sprintf( __('<span class="saved-sale"> Save: %s <em>(%s)</em></span>', 'woocommerce' ), $saving_price, $saving_percentage );
}}
return $price;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment