You are here

public function OrderSubscriber::onCancel in Commerce Shipping 8.2

Cancels the order's shipments when the order is canceled.

Parameters

\Drupal\state_machine\Event\WorkflowTransitionEvent $event: The transition event.

File

src/EventSubscriber/OrderSubscriber.php, line 47

Class

OrderSubscriber

Namespace

Drupal\commerce_shipping\EventSubscriber

Code

public function onCancel(WorkflowTransitionEvent $event) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event
    ->getEntity();
  if (!$this->shippingOrderManager
    ->hasShipments($order)) {
    return;
  }

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface $shipment */
  foreach ($order
    ->get('shipments')
    ->referencedEntities() as $shipment) {
    if (!$shipment
      ->getState()
      ->isTransitionAllowed('cancel')) {
      continue;
    }
    $shipment
      ->getState()
      ->applyTransitionById('cancel');
    $shipment
      ->save();
  }
}