This snippet allows you to display the percentage saving amount on sale products on the single product template and on the loop items.
/**
* Snippet Name: WooCommerce Display Percentage Saving Amount On Sale Products
* Snippet Author: ecommercehints.com
*/
// Simple Products
add_filter( 'woocommerce_get_price_html', 'ecommercehints_display_percentage_savings_amount_on_sale_simple_products', 10, 2 );
function ecommercehints_display_percentage_savings_amount_on_sale_simple_products( $price, $product ) {
if ( $product->is_on_sale() && $product->is_type( 'simple' ) ) {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$savings_calculation = $sale_price * 100 / $regular_price;
$savings_total = 100 - $savings_calculation;
$price .= '
You save ' . round($savings_total) . '%';
}
return $price;
}
// Variable Products (shows when variations have been selected)
add_filter( 'woocommerce_available_variation', 'ecommercehints_display_percentage_savings_amount_on_sale_variable_products', 10, 3 );
function ecommercehints_display_percentage_savings_amount_on_sale_variable_products( $data, $product, $variation ) {
if( $variation->is_on_sale() ) {
$savings_amount = $data['display_regular_price'] - $data['display_price'];
$percentage_amount = round( $saved_amount / $data['display_regular_price'] * 100 );
$data['price_html'] .= 'You save ' . $percentage_amount. '%';
}
return $data;
}