You are here

function commerce_license_commerce_order_item_delete in Commerce License 8.2

Implements hook_ENTITY_TYPE_delete() for commerce_order_item.

File

./commerce_license.module, line 55
Contains commerce_license.module.

Code

function commerce_license_commerce_order_item_delete(EntityInterface $entity) {

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $entity */

  // Delete licenses for order items which are removed from a cart.
  // Only act on order items with the license field.
  if (!$entity
    ->hasField('license') || $entity
    ->get('license')
    ->isEmpty()) {
    return;
  }

  // Only act if there is a license.
  if (empty($entity->license->entity)) {
    return;
  }

  // Only act if the order is in draft. If the order is being edited when
  // complete, the license is probably active.
  if ($entity
    ->getOrder() && $entity
    ->getOrder()
    ->getState()
    ->getId() != 'draft') {
    return;
  }

  // Delete the license.
  $license = $entity->license->entity;
  $license
    ->delete();
}