You are here

public function LateOrderProcessor::process in Commerce Shipping 8.2

Processes an order.

Parameters

\Drupal\commerce_order\Entity\OrderInterface $order: The order.

Overrides OrderProcessorInterface::process

File

src/LateOrderProcessor.php, line 41

Class

LateOrderProcessor
Completes the order refresh process for shipments.

Namespace

Drupal\commerce_shipping

Code

public function process(OrderInterface $order) {
  if (!$this->shippingOrderManager
    ->hasShipments($order)) {
    return;
  }

  /** @var \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */
  $shipments = $order
    ->get('shipments')
    ->referencedEntities();
  $single_shipment = count($shipments) === 1;
  foreach ($shipments as $shipment) {
    if ($shipment
      ->hasTranslationChanges()) {
      $shipment
        ->save();
    }
    if ($amount = $shipment
      ->getAmount()) {

      // Shipments without an amount are incomplete / unrated.
      $order
        ->addAdjustment(new Adjustment([
        'type' => 'shipping',
        'label' => $single_shipment ? t('Shipping') : $shipment
          ->getTitle(),
        'amount' => $amount,
        'source_id' => $shipment
          ->id(),
      ]));
      foreach ($shipment
        ->getAdjustments() as $adjustment) {
        if ($adjustment
          ->isLocked()) {

          // Locked shipment adjustments must be transferred unlocked
          // so that they're cleared at the beginning of order refresh.
          $adjustment = new Adjustment([
            'locked' => FALSE,
          ] + $adjustment
            ->toArray());
        }
        $order
          ->addAdjustment($adjustment);
      }
    }
  }
}