You are here

public function OrderSubscriber::onCancel in Commerce License 8.2

Reacts to an order being cancelled.

Parameters

\Drupal\state_machine\Event\WorkflowTransitionEvent $event: The event we subscribed to.

File

src/EventSubscriber/OrderSubscriber.php, line 120

Class

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

Namespace

Drupal\commerce_license\EventSubscriber

Code

public function onCancel(WorkflowTransitionEvent $event) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event
    ->getEntity();
  $license_order_items = $this
    ->getLicensableOrderItems($order);
  foreach ($license_order_items as $order_item) {

    // Get the license from the order item.
    $license = $order_item
      ->get('license')->entity;
    if (!$license) {
      continue;
    }

    // Cancel the license.
    $transition = $license
      ->getState()
      ->getWorkflow()
      ->getTransition('cancel');
    $license
      ->getState()
      ->applyTransition($transition);
    $license
      ->save();
  }
}