You are here

public function OrderSubscriber::onPlace in Commerce Shipping 8.2

Finalizes the order's shipments when the order is placed.

Only used if the workflow does not have a validation step. Otherwise the same logic is handled by onValidate().

Parameters

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

File

src/EventSubscriber/OrderSubscriber.php, line 73

Class

OrderSubscriber

Namespace

Drupal\commerce_shipping\EventSubscriber

Code

public function onPlace(WorkflowTransitionEvent $event) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $event
    ->getEntity();
  $to_state = $event
    ->getTransition()
    ->getToState();
  if ($to_state
    ->getId() != 'fulfillment' || !$this->shippingOrderManager
    ->hasShipments($order)) {
    return;
  }

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