/** * Process shipping method and save * * @param EventObserver $observer * @return void */ public function execute(EventObserver $observer) { if ($this->shipperDataHelper->getConfigValue('carriers/shipper/active')) { $request = $observer->getEvent()->getRequest(); $shippingMethods = $request->getPost('shipping_method', ''); if (!is_array($shippingMethods)) { return; } foreach ($shippingMethods as $addressId => $shippingMethod) { if (empty($shippingMethod)) { return; } $quote = $observer->getEvent()->getQuote(); $addresses = $quote->getAllShippingAddresses(); $shippingAddress = false; foreach ($addresses as $address) { if ($address->getId() == $addressId) { $shippingAddress = $address; break; } } $this->carrierGroupHelper->saveCarrierGroupInformation($shippingAddress, $shippingMethod); } } }
/** * Record order shipping information after order is placed * * @param EventObserver $observer * @return void */ public function execute(EventObserver $observer) { if ($this->shipperDataHelper->getConfigValue('carriers/shipper/active')) { $requestData = $observer->getRequestModel()->getPost(); if (isset($requestData['order'])) { $orderData = $requestData['order']; } $quote = $observer->getSession()->getQuote(); //if(!empty($orderData['shipping_method_flag'])) if (!empty($orderData['shipping_method'])) { $shippingMethod = $orderData['shipping_method']; $this->carrierGroupHelper->saveCarrierGroupInformation($quote->getShippingAddress(), $shippingMethod); } //} } }
/** *Set additional information for shipping address * * @param \Magento\Checkout\Model\ShippingInformationManagement $subject * @param callable $proceed * * @return \Magento\Checkout\Api\Data\PaymentDetailsInterface $paymentDetails * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ public function aroundSaveAddressInformation(\Magento\Checkout\Model\ShippingInformationManagement $subject, $proceed, $cartId, \Magento\Checkout\Api\Data\ShippingInformationInterface $addressInformation) { try { $carrierCode = $addressInformation->getShippingCarrierCode(); $methodCode = $addressInformation->getShippingMethodCode(); $shippingMethod = $carrierCode . '_' . $methodCode; $quote = $this->quoteRepository->getActive($cartId); $address = $quote->getShippingAddress(); $validation = $this->checkoutSession->getShipAddressValidation(); if (is_array($validation) && isset($validation['key'])) { if (isset($validation['validation_status'])) { $additionalDetail['address_valid'] = $validation['validation_status']; $address->setValidationStatus($validation['validation_status']); } if (isset($validation['destination_type'])) { $additionalDetail['destination_type'] = $validation['destination_type']; $address->setDestinationType($validation['destination_type']); } $this->checkoutSession->setShipAddressValidation(null); } $address->save(); $additionalDetail = new \Magento\Framework\DataObject(); $extAttributes = $addressInformation->getShippingAddress()->getExtensionAttributes(); //push out event so other modules can save their data TODO add carrier_group_id $this->eventManager->dispatch('shipperhq_additional_detail_checkout', ['address_extn_attributes' => $extAttributes, 'additional_detail' => $additionalDetail, 'carrier_code' => $carrierCode]); $additionalDetailArray = $additionalDetail->convertToArray(); $this->shipperLogger->postDebug('ShipperHQ Shipper', 'processing additional detail ', $additionalDetail); $this->carrierGroupHelper->saveCarrierGroupInformation($address, $shippingMethod, $additionalDetailArray); } catch (\Exception $e) { $this->shipperLogger->postCritical('Shipperhq_Shipper', 'Shipping Information Plugin', 'Exception raised ' . $e->getMessage()); } $result = $proceed($cartId, $addressInformation); if ($address->getCustomerId()) { $customerAddresses = $quote->getCustomer()->getAddresses(); foreach ($customerAddresses as $oneAddress) { if ($oneAddress->getId() == $address->getCustomerAddressId()) { if ($address->getValidationStatus()) { $oneAddress->setCustomAttribute('validation_status', $address->getValidationStatus()); } if ($address->getDestinationType()) { $oneAddress->setCustomAttribute('destination_type', $address->getDestinationType()); } $this->addressRepository->save($oneAddress); } } } return $result; }