diff --git a/src/Form/ShipmentForm.php b/src/Form/ShipmentForm.php index b6d31c7..c2bd109 100644 --- a/src/Form/ShipmentForm.php +++ b/src/Form/ShipmentForm.php @@ -4,6 +4,7 @@ namespace Drupal\commerce_shipping\Form; use Drupal\commerce\AjaxFormTrait; use Drupal\commerce_order\Entity\OrderInterface; +use Drupal\commerce_order\Adjustment; use Drupal\commerce_shipping\PackageTypeManagerInterface; use Drupal\commerce_shipping\ShipmentItem; use Drupal\Component\Datetime\TimeInterface; @@ -314,7 +315,29 @@ class ShipmentForm extends ContentEntityForm { // Check if the shipment amount has changed, if so we need to trigger // an order refresh so that the shipping adjustment gets adjusted. if ($form_state->get('original_amount') != $shipment->getAmount()) { - $order->setRefreshState(OrderInterface::REFRESH_ON_SAVE); + if ($order->getState()->getId() == 'draft') { + $order->setRefreshState(OrderInterface::REFRESH_ON_SAVE); + } + else { + // If the order is no longer in draft state then only update the shipping adjustment. + $single_shipment = count($order_shipments->referencedEntities()) === 1; + + foreach ($order->getAdjustments(['shipping']) as $shipping_adjustment) { + $order->removeAdjustment($shipping_adjustment); + } + + foreach ($order_shipments->referencedEntities() as $key => $order_shipment) { + if ($amount = $order_shipment->getAmount()) { + // Shipments without an amount are incomplete / unrated. + $order->addAdjustment(new Adjustment([ + 'type' => 'shipping', + 'label' => $single_shipment ? $this->t('Shipping') : $order_shipment->getTitle(), + 'amount' => $amount, + 'source_id' => $order_shipment->id(), + ])); + } + } + } $save_order = TRUE; }