You are here

public function BundleItemOrderItemSubscriber::saveOrderItemBackReference in Commerce Product Bundle 8

Saves the order item back reference to the bundle item order item.

Parameters

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

File

src/EventSubscriber/BundleItemOrderItemSubscriber.php, line 69

Class

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

Namespace

Drupal\commerce_product_bundle\EventSubscriber

Code

public function saveOrderItemBackReference(OrderItemEvent $event) {
  $order_item = $event
    ->getOrderItem();
  if ($order_item
    ->bundle() === 'commerce_product_bundle_default' && $order_item
    ->hasField('bundle_item_order_items')) {
    foreach ($order_item
      ->get('bundle_item_order_items')
      ->referencedEntities() as $bundle_item_order_item) {
      if ($bundle_item_order_item->order_item_id
        ->isEmpty()) {
        $bundle_item_order_item
          ->set('order_item_id', $order_item);
        $bundle_item_order_item
          ->save();
      }
    }
  }
}