You are here

protected function OrderSubscriber::createLicenseFromOrderItem in Commerce License 8.2

Creates the license using the given order item.

Parameters

\Drupal\commerce_order\Entity\OrderItemInterface $order_item: A licensable order item.

Return value

\Drupal\commerce_license\Entity\LicenseInterface The created license.

2 calls to OrderSubscriber::createLicenseFromOrderItem()
OrderSubscriber::onPaid in src/EventSubscriber/OrderSubscriber.php
Activates the licenses on order paid.
OrderSubscriber::onPlace in src/EventSubscriber/OrderSubscriber.php
Reacts to an order being placed.

File

src/EventSubscriber/OrderSubscriber.php, line 186

Class

OrderSubscriber
Changes a license's state in sync with an order's workflow.

Namespace

Drupal\commerce_license\EventSubscriber

Code

protected function createLicenseFromOrderItem(OrderItemInterface $order_item) {

  /** @var \Drupal\commerce_license\LicenseStorageInterface $license_storage */
  $license_storage = $this->entityTypeManager
    ->getStorage('commerce_license');
  $license = $license_storage
    ->createFromOrderItem($order_item);

  // The license is "pending" until it gets activated, either when the order
  // gets paid, or if the license should be activated on order place.
  $license
    ->set('state', 'pending');
  $license
    ->save();

  // Set the license field on the order item so we have a reference
  // and can get hold of it in later events.
  $order_item->license = $license
    ->id();
  $order_item
    ->save();
  return $license;
}