Merge pull request #17710 from woocommerce/fix/17697-coupon-spend-incl-taxes

Coupon min/max spend based on displayed subtotal
This commit is contained in:
Mike Jolley 2017-11-16 10:59:43 +00:00 committed by GitHub
commit 541dde95b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 19 additions and 6 deletions

View File

@ -117,7 +117,7 @@ class WC_Meta_Box_Coupon_Data {
'id' => 'minimum_amount',
'label' => __( 'Minimum spend', 'woocommerce' ),
'placeholder' => __( 'No minimum', 'woocommerce' ),
'description' => __( 'This field allows you to set the minimum spend (subtotal, including taxes) allowed to use the coupon.', 'woocommerce' ),
'description' => __( 'This field allows you to set the minimum spend (subtotal) allowed to use the coupon.', 'woocommerce' ),
'data_type' => 'price',
'desc_tip' => true,
) );
@ -127,7 +127,7 @@ class WC_Meta_Box_Coupon_Data {
'id' => 'maximum_amount',
'label' => __( 'Maximum spend', 'woocommerce' ),
'placeholder' => __( 'No maximum', 'woocommerce' ),
'description' => __( 'This field allows you to set the maximum spend (subtotal, including taxes) allowed when using the coupon.', 'woocommerce' ),
'description' => __( 'This field allows you to set the maximum spend (subtotal) allowed when using the coupon.', 'woocommerce' ),
'data_type' => 'price',
'desc_tip' => true,
) );

View File

@ -613,8 +613,7 @@ class WC_Discounts {
* @return bool
*/
protected function validate_coupon_minimum_amount( $coupon ) {
$subtotal = wc_remove_number_precision( array_sum( wp_list_pluck( $this->items, 'price' ) ) );
$subtotal = wc_remove_number_precision( $this->get_object_subtotal() );
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 );
@ -632,8 +631,7 @@ class WC_Discounts {
* @return bool
*/
protected function validate_coupon_maximum_amount( $coupon ) {
$subtotal = wc_remove_number_precision( array_sum( wp_list_pluck( $this->items, 'price' ) ) );
$subtotal = wc_remove_number_precision( $this->get_object_subtotal() );
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 );
@ -847,6 +845,21 @@ class WC_Discounts {
return true;
}
/**
* Get the object subtotal
*
* @return int
*/
protected function get_object_subtotal() {
if ( is_a( $this->object, 'WC_Cart' ) ) {
return wc_add_number_precision( $this->object->get_displayed_subtotal() );
} elseif ( is_a( $object, 'WC_Order' ) ) {
return wc_add_number_precision( $this->object->get_subtotal() );
} else {
return array_sum( wp_list_pluck( $this->items, 'price' ) );
}
}
/**
* Check if a coupon is valid.
*