Display Sale badge percentage for JetWooBuilder For Elementor
Display sale badge percentages for JetWooBuilder for Elementor.
// Display Sale badge percentage for JetWooBuilder For Elementor
add_filter('jet-woo-builder/template-functions/product-sale-flash', function($html, $product) {
if (is_object($product) && method_exists($product, 'is_on_sale') && $product->is_on_sale()) {
$percentage = 0;
if (!$product->is_type('variable')) {
$regular_price = $product->get_regular_price();
$sale_price = $product->get_sale_price();
$discounted_price = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', false, $product, 1, 0, 'discounted_price', true, false);
if ($discounted_price !== false) {
$sale_price = $discounted_price;
}
if ($regular_price != '' && $sale_price != '') {
$percentage = round((($regular_price - $sale_price) / $regular_price) * 100);
}
} elseif ($product->is_type('variable') && function_exists('wc_get_product')) {
$max_percentage = 0;
$available_variations = $product->get_available_variations();
foreach($available_variations as $variation_data) {
$product_variation = wc_get_product($variation_data['variation_id']);
$regular_price = $product_variation->get_regular_price();
$sale_price = $product_variation->get_price() ? $product_variation->get_price() : 0;
$discounted_price = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', false, $product_variation, 1, 0, 'discounted_price', true, false);
if ($discounted_price !== false) {
$sale_price = $discounted_price;
}
$percentage = round((($regular_price - $sale_price) / $regular_price) * 100);
if ($percentage > $max_percentage) {
$max_percentage = $percentage;
}
}
$percentage = $max_percentage;
}
if ($percentage > 0) {
return sprintf('<div class="jet-woo-product-badge jet-woo-product-badge__sale">%s</div>', $percentage . '%');
}
}
return $html;
}, 10, 2);