Home › Forums › Azuma Pro › Sale banner integration with Woo Discount Rules › Reply To: Sale banner integration with Woo Discount Rules
April 10, 2020 at 3:48 pm
#10146
Andy
Keymaster
Hi Frederik,
These are the two functions that output the standard sale price % discount;
To change either of these functions, you will need to copy the code – making any changes to use your custom discount rules – into the functions.php file of a child theme.
This azuma_before_loop_sale_flash
function just adds the start of the theme’s div wrapper, so no need to modify this if you don’t need to change it:
function azuma_before_loop_sale_flash() {
global $product;
if ( $product->is_on_sale() ) {
echo '<div class="sale-flash">';
}
}
This azuma_after_loop_sale_flash
function calculates the discount % and outputs it, so you would need to remove the code that calculates the discount and replace with your own code:
function azuma_after_loop_sale_flash() {
global $product;
if ( $product->is_on_sale() ) {
if ( ! $product->is_type( 'variable' ) && $product->get_regular_price() && $product->get_sale_price() ) {
$discount_price = $product->get_regular_price() - $product->get_sale_price();
if ( $discount_price > 0 ) {
$max_percentage = ( $discount_price / $product->get_regular_price() ) * 100;
} else {
$max_percentage = 0;
}
} else {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
$percentage = '';
if ( $price != 0 && ! empty( $sale ) ) {
$percentage = ( $price - $sale ) / $price * 100;
}
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
echo '<br /><span class="sale-percentage">-' . esc_attr( round($max_percentage) ) . '%</span>';
echo '</div>';
}
}
for example:
function azuma_after_loop_sale_flash() {
global $product;
if ( $product->is_on_sale() ) {
/*
YOUR CUSTOM CODE TO CALCULATE YOUR DISCOUNT
*/
echo '<br /><span class="sale-percentage">-' . esc_attr( $your_calculated_discount ) . '%</span>';
echo '</div>';
}
}