From 8adb4741493adef1d06214f7302789e5eccf33a7 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 2 Apr 2020 15:00:06 +0000 Subject: [PATCH 1/2] Enforce per user coupon usage limit for guest user checkout. In a previous commit, a regression was introduced where we were no longer checking for usage limit of guest user in an install. This commit adds back that check. --- includes/class-wc-cart.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/includes/class-wc-cart.php b/includes/class-wc-cart.php index 0e2e5db7dd6..fd05441ed43 100644 --- a/includes/class-wc-cart.php +++ b/includes/class-wc-cart.php @@ -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 ); + } + } } } } From 0af20fe4ef17fe39026ad389fb7d1ab800338232 Mon Sep 17 00:00:00 2001 From: vedanshujain Date: Thu, 2 Apr 2020 15:04:57 +0000 Subject: [PATCH 2/2] Add unit test for guest checkout with per user coupon usage limit. --- tests/unit-tests/checkout/checkout.php | 30 ++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/unit-tests/checkout/checkout.php b/tests/unit-tests/checkout/checkout.php index 874dd648844..fb2ad7ea82d 100644 --- a/tests/unit-tests/checkout/checkout.php +++ b/tests/unit-tests/checkout/checkout.php @@ -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. *