You are here

public static function Coupon::postDelete in Commerce Core 8.2

Acts on deleted entities before the delete hook is invoked.

Used after the entities are deleted but before invoking the delete hook.

Parameters

\Drupal\Core\Entity\EntityStorageInterface $storage: The entity storage object.

\Drupal\Core\Entity\EntityInterface[] $entities: An array of entities.

Overrides EntityBase::postDelete

File

modules/promotion/src/Entity/Coupon.php, line 217

Class

Coupon
Defines the Coupon entity.

Namespace

Drupal\commerce_promotion\Entity

Code

public static function postDelete(EntityStorageInterface $storage, array $entities) {

  // Delete the related usage.

  /** @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::entityTypeManager()
    ->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();
  }
}