Round discount value for both Price and Cart Rules
Round discount values for both price and cart rules.
// Round discount value for both Price and Cart Rules (Ex: 9.99 round down to 9.95 and 9.94 round down to 9.90)
if (!function_exists('advanced_woo_discount_rules_round_discount_price')) {
function advanced_woo_discount_rules_round_discount_price($price) {
$rounded_price = wc_round_discount($price, 1);
$diff = wc_round_discount($price - $rounded_price, 2);
if ($diff == 0.05) {
return $rounded_price + 0.05;
} else if ($diff >= 0) {
return $rounded_price;
} else {
return $rounded_price - 0.05;
}
}
}
add_filter('advanced_woo_discount_rules_discount_prices_of_product', function ($discount_prices, $product, $quantity, $cart_item) {
//FOR CHANGING DISCOUNTED_PRICE you can change it in $discount_prices['discounted_price']
if (isset($discount_prices['discounted_price'])) {
$discount_prices['discounted_price'] = advanced_woo_discount_rules_round_discount_price($discount_prices['discounted_price']);
}
if (isset($discount_prices['discounted_price_with_tax'])) {
$discount_prices['discounted_price_with_tax'] = advanced_woo_discount_rules_round_discount_price($discount_prices['discounted_price_with_tax']);
}
if (isset($discount_prices['discount_lines'])) {
foreach ($discount_prices['discount_lines'] as $key => $value) {
if ($key !== 'non_applied') {
$line_discount = $discount_prices['discount_lines'][$key]['discounted_price'];
$discount_prices['discount_lines'][$key]['discounted_price'] = advanced_woo_discount_rules_round_discount_price($line_discount);
}
}
}
return $discount_prices;
}, 10, 4);
add_filter('advanced_woo_discount_rules_discounts_of_each_rule', function ($discounts, $rule, $product_price) {
if (!empty($discounts) && is_array($discounts)) {
$discounts = array_map(
function ($v) {
return advanced_woo_discount_rules_round_discount_price($v);
}, $discounts);
}
return $discounts;
}, 10, 3);
add_filter('advanced_woo_discount_rules_coupon_value', function ($discount_value, $label, $cart_item_keys) {
return advanced_woo_discount_rules_round_discount_price($discount_value);
}, 10, 3);