Skip to content

Instantly share code, notes, and snippets.

@yratof
Last active March 23, 2017 21:44
Show Gist options
  • Save yratof/4968eeffea7f51b46f94 to your computer and use it in GitHub Desktop.
Save yratof/4968eeffea7f51b46f94 to your computer and use it in GitHub Desktop.
Woocommerce: Show % saved
<?php
/**
* Add save percent next to sale item prices,
* wrapped in a span + strong for CSS
* targeting with .saving{} and .saving strong{}
*/
add_filter( 'woocommerce_sale_price_html', 'woocommerce_sales_price_saving', 10, 2 );
function woocommerce_sales_price_saving( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
return $price . sprintf( __('<span class="saving">Save <strong>%s</strong></span>', 'woocommerce' ), $percentage . '%' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment