From bb6ffad09a55b28c706513830d164bc41127b47f Mon Sep 17 00:00:00 2001 From: James Allan Date: Fri, 5 May 2023 16:24:54 +1000 Subject: [PATCH 1/2] Set up coupons for manual discounts for the full amount --- includes/class-wcs-cart-renewal.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wcs-cart-renewal.php b/includes/class-wcs-cart-renewal.php index 158a541a3..465213e55 100644 --- a/includes/class-wcs-cart-renewal.php +++ b/includes/class-wcs-cart-renewal.php @@ -1350,14 +1350,14 @@ public function maybe_update_shipping_packages( $packages ) { * @since 1.0.0 - Migrated from WooCommerce Subscriptions v2.4.3 */ public function setup_discounts( $order ) { - $order_discount = $order->get_total_discount(); + $order_discount = $order->get_total_discount() + $order->get_discount_tax(); $coupon_items = $order->get_items( 'coupon' ); if ( empty( $order_discount ) && empty( $coupon_items ) ) { return; } - $total_coupon_discount = floatval( array_sum( wc_list_pluck( $coupon_items, 'get_discount' ) ) ); + $total_coupon_discount = floatval( array_sum( wc_list_pluck( $coupon_items, 'get_discount' ) ) + array_sum( wc_list_pluck( $coupon_items, 'get_discount_tax' ) ) ); $coupons = array(); // If the order total discount is different from the discount applied from coupons we have a manually applied discount. From dd46dbf8661a319c15b3ea74075562d87d626358 Mon Sep 17 00:00:00 2001 From: James Allan Date: Fri, 5 May 2023 16:35:51 +1000 Subject: [PATCH 2/2] Allow for 1 cent delta when comparing coupon total and order discount total --- includes/class-wcs-cart-renewal.php | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/includes/class-wcs-cart-renewal.php b/includes/class-wcs-cart-renewal.php index 465213e55..f5035aa13 100644 --- a/includes/class-wcs-cart-renewal.php +++ b/includes/class-wcs-cart-renewal.php @@ -1360,8 +1360,11 @@ public function setup_discounts( $order ) { $total_coupon_discount = floatval( array_sum( wc_list_pluck( $coupon_items, 'get_discount' ) ) + array_sum( wc_list_pluck( $coupon_items, 'get_discount_tax' ) ) ); $coupons = array(); - // If the order total discount is different from the discount applied from coupons we have a manually applied discount. - $order_has_manual_discount = $order_discount !== $total_coupon_discount; + // If the order total discount is different from the discount applied from coupons then we have a manually applied discount. + $delta_threshold = 1; // Allow for floating point rounding errors. + + // Add the rounding number precision (eg convert to cents) to compare the order discount and coupon discount at the same precision. + $order_has_manual_discount = abs( wc_add_number_precision( $order_discount ) - wc_add_number_precision( $total_coupon_discount ) ) > $delta_threshold; // Get all coupon line items as coupon objects. if ( ! empty( $coupon_items ) ) {