Skip to content

Instantly share code, notes, and snippets.

@vandanojan
Last active September 9, 2021 08:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vandanojan/d84bcf0e2e1714970daedc522ad05fcc to your computer and use it in GitHub Desktop.
Save vandanojan/d84bcf0e2e1714970daedc522ad05fcc to your computer and use it in GitHub Desktop.
Display Woocommerce Discount Price as Saved Amount/Percentage for Simple/Variable Products
<?php
/********* Woocommerce Simple / Variable Product Discount (Saving Amount or Percentage) *********/
/*
* Depeding on your product type (SIMPLE or VARIABLE) and the DISCOUNT type you prefer to be displayed under the price
(SAVED AMOUNT or SAVING PERCENTAGE), simply choose the proper code snippet as follows and add to your theme's functions.php.
*/
// Simple Product - Saved Amount
add_action( 'woocommerce_single_product_summary', 'simple_product_saving_amount', 11 );
function simple_product_saving_amount() {
global $product;
if( $product->is_type('simple') && $product->is_on_sale() ) {
$regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
$active_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );
$saved_amount = $regular_price - $active_price;
echo '<p id="saving_total_price">'. __("You Saved") .': ' . wc_price($saved_amount) . ' </p>';
}
}
// Simple Product - Saving Percentage
add_action( 'woocommerce_single_product_summary', 'simple_product_saving_percentage', 11 );
function simple_product_saving_percentage() {
global $product;
if( $product->is_type('simple') && $product->is_on_sale() ) {
$regular_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_regular_price() ) );
$active_price = (float) wc_get_price_to_display( $product, array('price' => $product->get_sale_price() ) );
$saved_amount = $regular_price - $active_price;
$percentage = round( $saved_amount / $regular_price * 100 );
echo '<p id="saving_total_price">'. __("You Saved") .': '.$percentage.'% </p>';
}
}
// Variable Product - Saved Amount
add_filter( 'woocommerce_available_variation', 'variable_product_saving_amount', 10, 3 );
function variable_product_saving_amount( $data, $product, $variation ) {
if( $variation->is_on_sale() ) {
$saved_amount = $data['display_regular_price'] - $data['display_price'];
$data['price_html'] .= '<p id="saving_total_price">'. __("You Saved") .': ' . wc_price($saved_amount) . '</p>';
}
return $data;
}
// Variable Product - Saving Percentage
add_filter( 'woocommerce_available_variation', 'variable_product_saving_percentage', 10, 3 );
function variable_product_saving_percentage( $data, $product, $variation ) {
if( $variation->is_on_sale() ) {
$saved_amount = $data['display_regular_price'] - $data['display_price'];
$percentage = round( $saved_amount / $data['display_regular_price'] * 100 );
$data['price_html'] .= '<p id="saving_total_price">'. __("You Saved") .': '.$percentage.'%</p>';
}
return $data;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment