From ae0f5fc76dd0ea05724aad19132820b1cab094b8 Mon Sep 17 00:00:00 2001 From: Caleb Burks <19caleb95@gmail.com> Date: Thu, 22 Feb 2018 14:33:06 -0500 Subject: [PATCH 1/3] Ensure coupon amount is not empty --- includes/class-wc-coupon.php | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/includes/class-wc-coupon.php b/includes/class-wc-coupon.php index a8e5863809e..dbc44cb9bc1 100644 --- a/includes/class-wc-coupon.php +++ b/includes/class-wc-coupon.php @@ -459,13 +459,21 @@ class WC_Coupon extends WC_Legacy_Coupon { * @throws WC_Data_Exception */ public function set_amount( $amount ) { + $amount = wc_format_decimal( $amount ); + + if ( ! is_numeric( $amount ) ) { + $amount = 0; + } + if ( $amount < 0 ) { $this->error( 'coupon_invalid_amount', __( 'Invalid discount amount', 'woocommerce' ) ); } + if ( 'percent' === $this->get_discount_type() && $amount > 100 ) { $this->error( 'coupon_invalid_amount', __( 'Invalid discount amount', 'woocommerce' ) ); } - $this->set_prop( 'amount', wc_format_decimal( $amount ) ); + + $this->set_prop( 'amount', $amount ); } /** From 9ba4ae7ad7d6dfef9c443510bed7e2c434c87d8d Mon Sep 17 00:00:00 2001 From: Caleb Burks <19caleb95@gmail.com> Date: Thu, 22 Feb 2018 14:39:37 -0500 Subject: [PATCH 2/3] Remove unnecessary conditional --- includes/class-wc-discounts.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/includes/class-wc-discounts.php b/includes/class-wc-discounts.php index 9c7daf4f701..93205a3e00f 100644 --- a/includes/class-wc-discounts.php +++ b/includes/class-wc-discounts.php @@ -342,8 +342,7 @@ class WC_Discounts { $limit_usage_qty = $coupon->get_limit_usage_to_x_items(); } - // Because get_amount() could return an empty string, let's be sure to set our local variable to a known good value. - $coupon_amount = ( '' === $coupon->get_amount() ) ? 0 : $coupon->get_amount(); + $coupon_amount = $coupon->get_amount(); foreach ( $items_to_apply as $item ) { // Find out how much price is available to discount for the item. @@ -400,7 +399,7 @@ class WC_Discounts { */ protected function apply_coupon_fixed_product( $coupon, $items_to_apply, $amount = null ) { $total_discount = 0; - $amount = $amount ? $amount: wc_add_number_precision( $coupon->get_amount() ); + $amount = $amount ? $amount : wc_add_number_precision( $coupon->get_amount() ); $limit_usage_qty = 0; $applied_count = 0; From 4a4aa84e266d78f3bfc03854deaa94c90c225964 Mon Sep 17 00:00:00 2001 From: Caleb Burks <19caleb95@gmail.com> Date: Thu, 22 Feb 2018 15:06:53 -0500 Subject: [PATCH 3/3] Add a test --- tests/unit-tests/coupon/data-store.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/unit-tests/coupon/data-store.php b/tests/unit-tests/coupon/data-store.php index 82eebd3baf9..f4075926139 100644 --- a/tests/unit-tests/coupon/data-store.php +++ b/tests/unit-tests/coupon/data-store.php @@ -80,6 +80,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case { $coupon = new WC_Coupon(); $coupon->set_code( $code ); $coupon->set_description( 'This is a test coupon.' ); + $coupon->set_amount( '' ); $coupon->set_usage_count( 5 ); $coupon->save(); $coupon_id = $coupon->get_id(); @@ -88,6 +89,7 @@ class WC_Tests_Coupon_Data_Store extends WC_Unit_Test_Case { $this->assertEquals( 5, $coupon_read->get_usage_count() ); $this->assertEquals( $code, $coupon_read->get_code() ); + $this->assertEquals( 0, $coupon->get_amount() ); $this->assertEquals( 'This is a test coupon.', $coupon_read->get_description() ); }