Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #2922147: Delete a coupon does not delete coupon reference from… #826

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions modules/promotion/src/Entity/Coupon.php
Expand Up @@ -164,6 +164,18 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
/** @var \Drupal\commerce_promotion\PromotionUsageInterface $usage */
$usage = \Drupal::service('commerce_promotion.usage');
$usage->deleteByCoupon($entities);
// Delete references to those coupons in promotions.
foreach ($entities as $coupon) {
$coupons_id[] = $coupon->id();
}
$promotions = \Drupal::service('entity_type.manager')->getStorage('commerce_promotion')->loadByProperties(['coupons' => $coupons_id]);
/** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
foreach ($promotions as $promotion) {
foreach ($entities as $entity) {
$promotion->removeCoupon($entity);
}
$promotion->save();
}
}

/**
Expand Down
8 changes: 8 additions & 0 deletions modules/promotion/tests/src/Kernel/Entity/PromotionTest.php
Expand Up @@ -45,6 +45,7 @@ protected function setUp() {
$this->installEntitySchema('commerce_order_item');
$this->installEntitySchema('commerce_promotion');
$this->installEntitySchema('commerce_promotion_coupon');
$this->installSchema('commerce_promotion', ['commerce_promotion_usage']);
$this->installConfig([
'profile',
'commerce_order',
Expand Down Expand Up @@ -152,6 +153,13 @@ public function testPromotion() {
$promotion->addCoupon($coupon1);
$this->assertTrue($promotion->hasCoupon($coupon1));

// Check Coupon::postDelete() remove Coupon reference from promotion.
$promotion->save();
$promotion = $this->reloadEntity($promotion);
$this->assertEquals($promotion->id(), 1);
$coupon1->delete();
$this->assertFalse($promotion->hasCoupon($coupon1));

$promotion->setUsageLimit(10);
$this->assertEquals(10, $promotion->getUsageLimit());

Expand Down