You are here

public function OrderSubscriber::onPaid in Commerce License 8.2

Activates the licenses on order paid.

Parameters

\Drupal\commerce_order\Event\OrderEvent $event: The order event.

File

src/EventSubscriber/OrderSubscriber.php, line 53

Class

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

Namespace

Drupal\commerce_license\EventSubscriber

Code

public function onPaid(OrderEvent $event) {
  $order = $event
    ->getOrder();
  $licensable_order_items = $this
    ->getLicensableOrderItems($order);
  foreach ($licensable_order_items as $order_item) {

    /** @var \Drupal\commerce_license\Entity\LicenseInterface $license */
    $license = $order_item
      ->get('license')->entity;

    // We don't need to do anything if there is already an active license
    // referenced by this order item.
    if ($license && $license
      ->getState()
      ->getId() === 'active') {
      continue;
    }
    if (!$license) {
      $license = $this
        ->createLicenseFromOrderItem($order_item);
    }
    $license
      ->set('state', 'active');
    $license
      ->save();
  }
}