When creating a new promotion, the Add new Coupon button fails.

The log gives:
TypeError: Argument 1 passed to Drupal\commerce_promotion\PromotionUsage::getUsage() must implement interface Drupal\commerce_promotion\Entity\PromotionInterface, null given, called in /Applications/MAMP/htdocs/uctd8/web/modules/contrib/commerce/modules/promotion/src/Plugin/Field/FieldWidget/UsageLimitWidget.php on line 82

It looks like the newly created Coupon entity has no Promotion ID set yet, when getUsage is called.

A solution would be to test for this before calling getUsage in UsageLimitWidget.php:

if ($promotion = $entity->getPromotion()) {
    $usage = $this->usage->getUsage($promotion, $entity);
}
else {
    $usage = 0;
}

Comments

finne created an issue. See original summary.

finne’s picture

finne’s picture

Assigned: Unassigned » finne
finne’s picture

Status: Active » Needs review
bojanz’s picture

Ah yes, my code works only if the promotion was saved before creating the coupon.

finne’s picture

On my install this never worked, neither new nor previously saved promotions...

bojanz’s picture

Title: Create new Coupon fails in UI » UsageLimitWidget should not load usage for unsaved entities

Cause it's the same bug. Retitling. We're obviously missing test coverage.

  • bojanz committed 9897285 on 8.x-2.x
    Issue #2884849: UsageLimitWidget should not load usage for unsaved...
bojanz’s picture

Status: Needs review » Fixed

Pushed a fix. Thanks!

Status: Fixed » Closed (fixed)

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