Currently we can enter two coupons with the same names. And the code just gets first one from entity query made for coupons and apply it.

/**
   * {@inheritdoc}
   */
  public function loadByCode($code) {
    $coupons = $this->loadByProperties(['code' => $code, 'status' => TRUE]);

    return reset($coupons);
  }

Comments

sumanthkumarc created an issue. See original summary.

bojanz’s picture

The CouponCodeConstraint checks that the code is unique when a coupon is validated.
Maybe we're running into an Inline Entity Form bug that's causing it to be skipped?

sumanthkumarc’s picture

@bojanz sorry i didn't specify the case, i am trying to create a coupon programmatically and add it to promotion.

   $coupon = Coupon::create([
      'promotion_id' => $promotion->id(),
      'code' => $coupon_code,
    ]);

    $coupon->save();

it doesn't throw any exceptions or errors in this case.

bojanz’s picture

Category: Feature request » Support request
Status: Active » Fixed

You need to run $violations = $coupon->validate(); and then check that. Drupal doesn't run validation on save (which is silly, I know)

sumanthkumarc’s picture

Title: Coupon names are not enforced for uniqueness » Coupon names are not enforced for uniqueness when created programatically

just changing the title, incase someone gets into similar condition.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.