From 5f2bc29e3db05191ee0da8c3a0829d9fd46237dd Mon Sep 17 00:00:00 2001 From: Ron Rennick Date: Tue, 15 Jan 2019 10:17:31 -0400 Subject: [PATCH 1/5] include taxes in subtotal when coupon price includes taxes --- includes/class-wc-discounts.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index fea4eead963..c4e931f6d7f 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -664,6 +664,11 @@ class WC_Discounts { */ protected function validate_coupon_minimum_amount( $coupon ) { $subtotal = wc_remove_number_precision( $this->get_object_subtotal() ); + + if ( $this->object->get_prices_include_tax() ) { + $subtotal += round( $this->object->get_total_tax(), wc_get_price_decimals() ); + } + if ( $coupon->get_minimum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_minimum_amount', $coupon->get_minimum_amount() > $subtotal, $coupon, $subtotal ) ) { /* translators: %s: coupon minimum amount */ throw new Exception( sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_minimum_amount() ) ), 108 ); From acf46fa28dbc5a1d3250db13575f3f2a33a1cd90 Mon Sep 17 00:00:00 2001 From: Ron Rennick Date: Tue, 15 Jan 2019 10:21:21 -0400 Subject: [PATCH 2/5] phpcs sniff fixes for class-wc-discounts.php --- includes/class-wc-discounts.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index c4e931f6d7f..94866123fe0 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -968,9 +968,13 @@ class WC_Discounts { */ $message = apply_filters( 'woocommerce_coupon_error', is_numeric( $e->getMessage() ) ? $coupon->get_coupon_error( $e->getMessage() ) : $e->getMessage(), $e->getCode(), $coupon ); - return new WP_Error( 'invalid_coupon', $message, array( - 'status' => 400, - ) ); + return new WP_Error( + 'invalid_coupon', + $message, + array( + 'status' => 400, + ) + ); } return true; } From d654107cb012de2d2625f9f9dcb33feb1f89a134 Mon Sep 17 00:00:00 2001 From: Ron Rennick Date: Tue, 15 Jan 2019 10:33:57 -0400 Subject: [PATCH 3/5] include taxes in subtotal when validating tax inclusive coupon maximum as well --- includes/class-wc-discounts.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index 94866123fe0..a00499dc875 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -687,6 +687,11 @@ class WC_Discounts { */ protected function validate_coupon_maximum_amount( $coupon ) { $subtotal = wc_remove_number_precision( $this->get_object_subtotal() ); + + if ( $this->object->get_prices_include_tax() ) { + $subtotal += round( $this->object->get_total_tax(), wc_get_price_decimals() ); + } + if ( $coupon->get_maximum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_maximum_amount', $coupon->get_maximum_amount() < $subtotal, $coupon ) ) { /* translators: %s: coupon maximum amount */ throw new Exception( sprintf( __( 'The maximum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_maximum_amount() ) ), 112 ); From 0f94bed1ec53d7668a560c9088db639fa066a354 Mon Sep 17 00:00:00 2001 From: Ron Rennick Date: Tue, 15 Jan 2019 11:33:44 -0400 Subject: [PATCH 4/5] only add tax when comparing coupon against order --- includes/class-wc-discounts.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index a00499dc875..8b39a8d992e 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -665,7 +665,7 @@ class WC_Discounts { protected function validate_coupon_minimum_amount( $coupon ) { $subtotal = wc_remove_number_precision( $this->get_object_subtotal() ); - if ( $this->object->get_prices_include_tax() ) { + if ( is_a( $this->object, 'WC_Order' ) && $this->object->get_prices_include_tax() ) { $subtotal += round( $this->object->get_total_tax(), wc_get_price_decimals() ); } @@ -688,7 +688,7 @@ class WC_Discounts { protected function validate_coupon_maximum_amount( $coupon ) { $subtotal = wc_remove_number_precision( $this->get_object_subtotal() ); - if ( $this->object->get_prices_include_tax() ) { + if ( is_a( $this->object, 'WC_Order' ) && $this->object->get_prices_include_tax() ) { $subtotal += round( $this->object->get_total_tax(), wc_get_price_decimals() ); } From 343a2179393271b0ee838fd029a039f4b9a31d6e Mon Sep 17 00:00:00 2001 From: Ron Rennick Date: Tue, 22 Jan 2019 10:46:49 -0400 Subject: [PATCH 5/5] move add order tax to get_object_subtotal() --- includes/class-wc-discounts.php | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index 8b39a8d992e..6f8502612a4 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -665,10 +665,6 @@ class WC_Discounts { protected function validate_coupon_minimum_amount( $coupon ) { $subtotal = wc_remove_number_precision( $this->get_object_subtotal() ); - if ( is_a( $this->object, 'WC_Order' ) && $this->object->get_prices_include_tax() ) { - $subtotal += round( $this->object->get_total_tax(), wc_get_price_decimals() ); - } - if ( $coupon->get_minimum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_minimum_amount', $coupon->get_minimum_amount() > $subtotal, $coupon, $subtotal ) ) { /* translators: %s: coupon minimum amount */ throw new Exception( sprintf( __( 'The minimum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_minimum_amount() ) ), 108 ); @@ -688,10 +684,6 @@ class WC_Discounts { protected function validate_coupon_maximum_amount( $coupon ) { $subtotal = wc_remove_number_precision( $this->get_object_subtotal() ); - if ( is_a( $this->object, 'WC_Order' ) && $this->object->get_prices_include_tax() ) { - $subtotal += round( $this->object->get_total_tax(), wc_get_price_decimals() ); - } - if ( $coupon->get_maximum_amount() > 0 && apply_filters( 'woocommerce_coupon_validate_maximum_amount', $coupon->get_maximum_amount() < $subtotal, $coupon ) ) { /* translators: %s: coupon maximum amount */ throw new Exception( sprintf( __( 'The maximum spend for this coupon is %s.', 'woocommerce' ), wc_price( $coupon->get_maximum_amount() ) ), 112 ); @@ -916,7 +908,8 @@ class WC_Discounts { if ( is_a( $this->object, 'WC_Cart' ) ) { return wc_add_number_precision( $this->object->get_displayed_subtotal() ); } elseif ( is_a( $this->object, 'WC_Order' ) ) { - return wc_add_number_precision( $this->object->get_subtotal() ); + $subtotal = wc_add_number_precision( $this->object->get_subtotal() ); + return $this->object->get_prices_include_tax() ? $subtotal + round( $this->object->get_total_tax(), wc_get_price_decimals() ) : $subtotal; } else { return array_sum( wp_list_pluck( $this->items, 'price' ) ); }