You are here

public function ShipmentSubscriber::onShipmentDelete in Commerce Shipping 8.2

Reacts to a shipment being deleted.

When a shipment gets deleted, ensure the order no longer references it on destruct(), and make sure the adjustments added by this shipment are removed. The reason why we're queuing this is to ensure we don't save the order during an order refresh which potentially causes data loss.

Parameters

\Drupal\commerce_shipping\Event\ShipmentEvent $event: The shipment event.

File

src/EventSubscriber/ShipmentSubscriber.php, line 90

Class

ShipmentSubscriber

Namespace

Drupal\commerce_shipping\EventSubscriber

Code

public function onShipmentDelete(ShipmentEvent $event) {
  $shipment = $event
    ->getShipment();
  if (!$shipment
    ->getOrderId()) {
    return;
  }

  // Shipment adjustments are transferred "unlocked" to the order
  // (See LateOrderProcessor), we store them in static cache so we can
  // properly remove them on destruct() if they still exist on the order.
  $adjustments = array_map(function (Adjustment $adjustment) {
    if ($adjustment
      ->isLocked()) {
      $adjustment = new Adjustment([
        'locked' => FALSE,
      ] + $adjustment
        ->toArray());
    }
    return $adjustment;
  }, $shipment
    ->getAdjustments());

  // Because we wouldn't be able to load the shipment that is about to be
  // deleted on destruct, store the adjustments that must be removed as well
  // as the shipment ID.
  // See ::clearShipments().
  $this->shipmentsToClear[$shipment
    ->getOrderId()][] = [
    'adjustments' => (array) $adjustments,
    'id' => $shipment
      ->id(),
  ];
}