Calculate Discount Percentage from Product
Calculate discount percentage based on product price.
<?php
$_product = wc_get_product( $atts['id'] );
$discount = false;
if (has_filter('advanced_woo_discount_rules_get_product_discount_price_from_custom_price')) {
$discount = apply_filters('advanced_woo_discount_rules_get_product_discount_price_from_custom_price', 0, $_product, 1, 0, 'all', true, false);
if($discount !== false){
$percentage = ($discount['discounted_price'] / $discount['initial_price']) * 100;
}
}
if ($discount === false) {
$percentage = ($_product->get_price() / $_product->get_regular_price()) * 100;
}
echo 'for <span id="percent_val">' . abs(round($percentage, 0, PHP_ROUND_HALF_EVEN) - 100) . '%</span> off today';
?>