From a77735432b26c7a598dee22c0d57455142f7408c Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Mon, 18 Apr 2016 11:39:00 +0100 Subject: [PATCH] Use get_displayed_subtotal() to determine if conditions for coupons and free shipping is met. @claudiosmweb Closes #10711 --- includes/class-wc-cart.php | 22 +++++++++++++++++++ includes/class-wc-coupon.php | 4 ++-- .../class-wc-shipping-free-shipping.php | 6 +---- 3 files changed, 25 insertions(+), 7 deletions(-) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 4aab2337fbc..4d489408370 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -790,6 +790,28 @@ class WC_Cart { return array_unique( $found_tax_classes ); } + /** + * Determines the value that the customer spent and the subtotal + * displayed, used for things like coupon validation. + * + * Since the coupon lines are displayed based on the TAX DISPLAY value + * of cart, this is used to determine the spend. + * + * If cart totals are shown including tax, use the subtotal. + * If cart totals are shown excluding tax, use the subtotal ex tax + * (tax is shown after coupons). + * + * @since 2.6.0 + * @return string + */ + public function get_displayed_subtotal() { + if ( 'incl' === $this->tax_display_cart ) { + return wc_format_decimal( $this->subtotal ); + } elseif ( 'excl' === $this->tax_display_cart ) { + return wc_format_decimal( $this->subtotal_ex_tax ); + } + } + /*-----------------------------------------------------------------------------------*/ /* Add to cart handling */ /*-----------------------------------------------------------------------------------*/ diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index 82cdd5c1827..c0e60f2932e 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -392,7 +392,7 @@ class WC_Coupon { * @throws Exception */ private function validate_minimum_amount() { - if ( $this->minimum_amount > 0 && wc_format_decimal( $this->minimum_amount ) > wc_format_decimal( WC()->cart->subtotal ) ) { + if ( $this->minimum_amount > 0 && apply_filters( 'woocommerce_coupon_validate_minimum_amount', wc_format_decimal( $this->minimum_amount ) > WC()->cart->get_displayed_subtotal(), $this ) ) { throw new Exception( self::E_WC_COUPON_MIN_SPEND_LIMIT_NOT_MET ); } } @@ -403,7 +403,7 @@ class WC_Coupon { * @throws Exception */ private function validate_maximum_amount() { - if ( $this->maximum_amount > 0 && wc_format_decimal( $this->maximum_amount ) < wc_format_decimal( WC()->cart->subtotal ) ) { + if ( $this->maximum_amount > 0 && apply_filters( 'woocommerce_coupon_validate_maximum_amount', wc_format_decimal( $this->maximum_amount ) <= WC()->cart->get_displayed_subtotal(), $this ) ) { throw new Exception( self::E_WC_COUPON_MAX_SPEND_LIMIT_MET ); } } diff --git a/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php b/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php index 14b425e3c23..f1f9322d16b 100644 --- a/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php +++ b/includes/shipping/free-shipping/class-wc-shipping-free-shipping.php @@ -112,11 +112,7 @@ class WC_Shipping_Free_Shipping extends WC_Shipping_Method { } if ( in_array( $this->requires, array( 'min_amount', 'either', 'both' ) ) && isset( WC()->cart->cart_contents_total ) ) { - if ( WC()->cart->prices_include_tax ) { - $total = WC()->cart->cart_contents_total + array_sum( WC()->cart->taxes ); - } else { - $total = WC()->cart->cart_contents_total; - } + $total = WC()->cart->get_displayed_subtotal(); if ( $total >= $this->min_amount ) { $has_met_min_amount = true;