Merge pull request #19098 from woocommerce/fix/19095-coupon-empty-cost

Ensure coupon amount is not empty
This commit is contained in:
Mike Jolley 2018-02-23 10:58:29 +00:00 committed by GitHub
commit dc4212cc15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 4 deletions

View File

@ -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 );
}
/**

View File

@ -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.

View File

@ -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() );
}