Merge pull request #26066 from woocommerce/fix/26002

Fix/26002
This commit is contained in:
Vedanshu Jain 2020-04-06 18:05:47 +05:30 committed by GitHub
commit e3eb30c509
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 0 deletions

View File

@ -1476,6 +1476,16 @@ class WC_Cart extends WC_Legacy_Cart {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_NOT_YOURS_REMOVED );
$this->remove_coupon( $code );
}
$coupon_usage_limit = $coupon->get_usage_limit_per_user();
if ( 0 < $coupon_usage_limit && 0 === get_current_user_id() ) {
// For guest, usage per user has not been enforced yet. Enforce it now.
$coupon_data_store = $coupon->get_data_store();
$billing_email = strtolower( sanitize_email( $billing_email ) );
if ( $coupon_data_store && $coupon_data_store->get_usage_by_email( $coupon, $billing_email ) >= $coupon_usage_limit ) {
$coupon->add_coupon_message( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED );
}
}
}
}
}

View File

@ -149,6 +149,36 @@ class WC_Tests_Checkout extends WC_Unit_Test_Case {
$this->assertEquals( $coupon_data_store->get_tentative_usage_count( $coupon->get_id() ), 1 );
}
/**
* Test usage limit for guest users uasge limit per user is set.
*
* @throws Exception When unable to create order.
*/
public function test_usage_limit_per_user_for_guest() {
wp_set_current_user( 0 );
wc_clear_notices();
$coupon_code = 'coupon4one';
$coupon = WC_Helper_Coupon::create_coupon(
$coupon_code,
array( 'usage_limit_per_user' => 1 )
);
$product = WC_Helper_Product::create_simple_product( true );
WC()->cart->add_to_cart( $product->get_id(), 1 );
WC()->cart->add_discount( $coupon->get_code() );
$checkout = WC_Checkout::instance();
$posted_data = array(
'billing_email' => 'a@b.com',
'payment_method' => 'dummy_payment_gateway',
);
$order_id = $checkout->create_order( $posted_data );
$this->assertNotWPError( $order_id );
WC()->cart->add_to_cart( $product->get_id(), 1 );
WC()->cart->add_discount( $coupon->get_code() );
WC()->cart->check_customer_coupons( $posted_data );
$this->assertTrue( wc_has_notice( $coupon->get_coupon_error( WC_Coupon::E_WC_COUPON_USAGE_LIMIT_REACHED ), 'error' ) );
}
/**
* Helper function to return 0.01.
*