diff --git a/src/Plugin/Commerce/CheckoutPane/ShippingInformation.php b/src/Plugin/Commerce/CheckoutPane/ShippingInformation.php index d284117..ae6f06f 100644 --- a/src/Plugin/Commerce/CheckoutPane/ShippingInformation.php +++ b/src/Plugin/Commerce/CheckoutPane/ShippingInformation.php @@ -7,12 +7,14 @@ use Drupal\commerce_checkout\Plugin\Commerce\CheckoutPane\CheckoutPaneBase; use Drupal\commerce_checkout\Plugin\Commerce\CheckoutFlow\CheckoutFlowInterface; use Drupal\commerce_shipping\OrderShipmentSummaryInterface; use Drupal\commerce_shipping\PackerManagerInterface; +use Drupal\commerce_shipping\ShippingMethodManager; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Render\Element; +use Drupal\profile\Entity\ProfileInterface; use function GuzzleHttp\Psr7\parse_request; use Symfony\Component\DependencyInjection\ContainerInterface; @@ -44,6 +46,14 @@ class ShippingInformation extends BillingInformationPaneBase { */ protected $orderShipmentSummary; + + /** + * The shipping method storage. + * + * @var \Drupal\commerce_shipping\ShippingMethodStorageInterface + */ + protected $shippingMethodStorage; + /** * Constructs a new ShippingInformation object. * @@ -62,11 +72,12 @@ class ShippingInformation extends BillingInformationPaneBase { * @param \Drupal\commerce_shipping\OrderShipmentSummaryInterface $order_shipment_summary * The order shipment summary. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager, PackerManagerInterface $packer_manager, OrderShipmentSummaryInterface $order_shipment_summary) { + public function __construct(array $configuration, $plugin_id, $plugin_definition, CheckoutFlowInterface $checkout_flow, EntityTypeManagerInterface $entity_type_manager, PackerManagerInterface $packer_manager, OrderShipmentSummaryInterface $order_shipment_summary, ShippingMethodManager $shipping_method) { parent::__construct($configuration, $plugin_id, $plugin_definition, $checkout_flow, $entity_type_manager); $this->packerManager = $packer_manager; $this->orderShipmentSummary = $order_shipment_summary; + $this->shippingMethodStorage = $this->entityTypeManager->getStorage('commerce_shipping_method'); } /** @@ -80,7 +91,8 @@ class ShippingInformation extends BillingInformationPaneBase { $checkout_flow, $container->get('entity_type.manager'), $container->get('commerce_shipping.packer_manager'), - $container->get('commerce_shipping.order_shipment_summary') + $container->get('commerce_shipping.order_shipment_summary'), + $container->get('plugin.manager.commerce_shipping_method') ); } @@ -311,8 +323,7 @@ class ShippingInformation extends BillingInformationPaneBase { $button_type = isset($triggering_element['#button_type']) ? $triggering_element['#button_type'] : ''; if ($input["shipping_information"]["shipping_profile"]["reuse_profile"] == "1" && $button_type == 'primary') { $shipping_profile = $form_state->get('shipping_profile'); - $shipments = $this->packerManager->packToShipments($this->order, $shipping_profile, []); - $this->order->shipments = $shipments; + $this->applyShipment($shipping_profile); return; } // Save the modified shipments. @@ -329,6 +340,7 @@ class ShippingInformation extends BillingInformationPaneBase { $shipments[] = $shipment; } $this->order->shipments = $shipments; + $this->order->save(); // Delete shipments that are no longer in use. $removed_shipment_ids = $pane_form['removed_shipments']['#value']; @@ -340,6 +352,31 @@ class ShippingInformation extends BillingInformationPaneBase { } /** + * Force apply shipping to order helper. + * + * @param \Drupal\profile\Entity\ProfileInterface $shipping_profile + * The shipping profile. + */ + protected function applyShipment(ProfileInterface $shipping_profile) { + /** \Drupal\commerce_shipping\Entity\ShipmentInterface[] $shipments */ + list($shipments, $removed_shipments) = $this->packerManager->packToShipments($this->order, $shipping_profile, []); + foreach ($shipments as $shipment) { + $shipping_methods = $this->shippingMethodStorage->loadMultipleForShipment($shipments[0]); + foreach ($shipping_methods as $shipping_method) { + $shipping_rates = $shipping_method->getPlugin() + ->calculateRates($shipment); + foreach ($shipping_rates as $shipping_rate) { + $shipping_method->getPlugin() + ->selectRate($shipment, $shipping_rate); + } + $shipment->setShippingMethod($shipping_method); + $shipment->setShippingProfile($shipping_profile); + } + } + $this->order->shipments = $shipments; + } + + /** * Gets the shipping profile. * * The shipping profile is assumed to be the same for all shipments.