You are here

public function BundleItemOrderItemSubscriber::deleteBundleItemOrderItem in Commerce Product Bundle 8

Deletes the bundle item order items of a deleted order item.

Parameters

\Drupal\commerce_order\Event\OrderItemEvent $event: The order item event.

File

src/EventSubscriber/BundleItemOrderItemSubscriber.php, line 47

Class

BundleItemOrderItemSubscriber
Deletes the bundle items order items if an order item gets deleted.

Namespace

Drupal\commerce_product_bundle\EventSubscriber

Code

public function deleteBundleItemOrderItem(OrderItemEvent $event) {

  /** @var \Drupal\commerce_order\Entity\OrderItemInterface $order_item */
  $order_item = $event
    ->getOrderItem();
  if ($order_item
    ->bundle() === 'commerce_product_bundle_default') {
    if ($order_item
      ->hasField('bundle_item_order_items')) {
      $bundle_item_order_items = [];
      foreach ($order_item
        ->get('bundle_item_order_items')
        ->referencedEntities() as $bundle_item_order_item) {
        $bundle_item_order_items[$bundle_item_order_item
          ->id()] = $bundle_item_order_item;
      }
      $this->bundleItemOrderItemStorage
        ->delete($bundle_item_order_items);
    }
  }
}