Commit 9e9dd5c3 authored by Jonathan Sacksick's avatar Jonathan Sacksick
Browse files

Issue #3278227 by jsacksick: Total per customer at the coupon level is ignored...

Issue #3278227 by jsacksick: Total per customer at the coupon level is ignored for unknown customers.
parent 40e90a4d
Loading
Loading
Loading
Loading
+6 −4
Original line number Diff line number Diff line
@@ -233,11 +233,13 @@ class Coupon extends CommerceContentEntityBase implements CouponInterface {
    if ($usage_limit && $usage_limit <= $usage->loadByCoupon($this)) {
      return FALSE;
    }

    // Only check customer usage when email address is known.
    if ($usage_limit_customer) {
      $email = $order->getEmail();
      if ($email && $usage_limit_customer <= $usage->loadByCoupon($this, $email)) {
      // Coupon cannot apply to orders without email addresses because of the
      // customer limit.
      if (!$email = $order->getEmail()) {
        return FALSE;
      }
      if ($usage_limit_customer <= $usage->loadByCoupon($this, $email)) {
        return FALSE;
      }
    }
+7 −3
Original line number Diff line number Diff line
@@ -174,16 +174,20 @@ class CouponTest extends OrderKernelTestBase {
    $this->assertFalse($coupon->available($this->order));

    // Test limit coupon usage by customer.
    $email = $this->order->getEmail();
    // Ensure that the usage limit per customer applies when the order email is
    // not known.
    $this->order->set('mail', NULL);
    $coupon->setCustomerUsageLimit(1);
    $coupon->setUsageLimit(0);
    $coupon->save();
    $coupon = $this->reloadEntity($coupon);
    $this->assertFalse($coupon->available($this->order));

    $this->order->setEmail($email);
    $this->assertFalse($coupon->available($this->order));

    $this->order->setEmail('another@example.com');
    $this->order->setRefreshState(Order::REFRESH_SKIP);
    $this->order->save();
    $this->order = $this->reloadEntity($this->order);
    $this->assertTrue($coupon->available($this->order));

    \Drupal::service('commerce_promotion.usage')->register($this->order, $promotion, $coupon);