Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save FrancoStino/32bfe6c443a55dbb51589d6439ed07bb to your computer and use it in GitHub Desktop.
Save FrancoStino/32bfe6c443a55dbb51589d6439ed07bb to your computer and use it in GitHub Desktop.
Display Discount Price Save @ Global Page - Product - Cart - WooCommerce
<?
/**
* @snippet Display Discount Percentage @ Loop Pages - WooCommerce
*/
// For simple products
add_filter( 'woocommerce_get_price_html', 'simple_product_saving_amount', 11, 2 );
function simple_product_saving_amount($price_html, $product) {
// global $product;
if( $product->is_type('simple') && $product->is_on_sale() && !is_cart() ) { // if product is: simple - on sale - not in cart
$regular_price = $product->get_regular_price(); // get regular price
$active_price = $product->get_sale_price(); // get sale price
$saved_amount = $regular_price - $active_price; // saved amount
$percentage = round( $saved_amount / $regular_price * 100 ); // percentage saved amount
$price_html .= '<div class="table-sale-perc"><div class="sale-perc" style="color: #fff">Risparmi: ' . wc_price($saved_amount) . ' ( '.$percentage.'% )</div></div>'; // print price with saved amout
return $price_html;
// ONLY FOR FIRST VISUAL VARIATION
}elseif ( $product->is_type( 'variable' ) && $product->is_on_sale() && !is_product() && !is_cart()) { // if product if product is: variable - on sale - not in product page - not in cart
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) { // get variation child
$variation = wc_get_product( $child_id ); // assign child
$price_html_variation = $variation->get_regular_price(); // get variation child price
$sale = $variation->get_sale_price(); // get variation child sale price
if ( $price_html_variation != 0 && ! empty( $sale ) ){ // if product is not sale
$saved_amount = $price_html_variation - $sale; // saved amount
$percentage = round( $saved_amount / $price_html_variation * 100 ); // percentage saved amount
}
if ( $saved_amount > $max_percentage ) {
$max_percentage = $saved_amount;
$price_html .= '<div class="table-sale-perc"><div class="sale-perc" style="color: #fff">Risparmi: ' . wc_price($max_percentage) . ' ( '.$percentage.'% )</div></div>';
}
if ( $max_percentage > 0 ){
return $price_html;
}
}
}
return $price_html;
}
// For product variations (on variable products)
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'];
$percentage = round( $saved_amount / $data['display_regular_price'] * 100 );
$data['price_html'] .= '<div class="table-sale-perc" style="margin-bottom:20px"><div class="sale-perc" style="color: #fff">Risparmi: ' . wc_price($saved_amount) . ' ( '.$percentage.'% )</div></div>';
}
return $data;
}
//------------------------------------ CSS --------------------------------------------//
<style>
.sale-perc {
background-color: green;
/*display: inline;*/
display: table;
width: fit-content;
padding: .2em .6em .3em;
font-size: 100%;
font-weight: bold;
color: #fff !important;
text-align: center;
border-radius: .25em;
margin-top: 10px;
}
.sale-perc .woocommerce-Price-amount.amount {
color: #fff !important;
}
</style>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment