You are here

class LateOrderProcessor in Commerce Shipping 8.2

Completes the order refresh process for shipments.

Saves any previously modified shipments. Transfers the shipment amount and adjustments to the order.

Runs after other order processors (promotion, tax, etc).

Hierarchy

Expanded class hierarchy of LateOrderProcessor

See also

\Drupal\commerce_shipping\EarlyOrderProcessor

1 string reference to 'LateOrderProcessor'
commerce_shipping.services.yml in ./commerce_shipping.services.yml
commerce_shipping.services.yml
1 service uses LateOrderProcessor
commerce_shipping.late_order_processor in ./commerce_shipping.services.yml
Drupal\commerce_shipping\LateOrderProcessor

File

src/LateOrderProcessor.php, line 19

Namespace

Drupal\commerce_shipping
View source
class LateOrderProcessor implements OrderProcessorInterface {

  /**
   * The shipping order manager.
   *
   * @var \Drupal\commerce_shipping\ShippingOrderManagerInterface
   */
  protected $shippingOrderManager;

  /**
   * Constructs a new LateOrderProcessor object.
   *
   * @param \Drupal\commerce_shipping\ShippingOrderManagerInterface $shipping_order_manager
   *   The shipping order manager.
   */
  public function __construct(ShippingOrderManagerInterface $shipping_order_manager) {
    $this->shippingOrderManager = $shipping_order_manager;
  }

  /**
   * {@inheritdoc}
   */
  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);
        }
      }
    }
  }

}

Members

Namesort descending Modifiers Type Description Overrides
LateOrderProcessor::$shippingOrderManager protected property The shipping order manager.
LateOrderProcessor::process public function Processes an order. Overrides OrderProcessorInterface::process
LateOrderProcessor::__construct public function Constructs a new LateOrderProcessor object.