/** * Return array of carriers. * If $isActiveOnlyFlag is set to true, will return only active carriers * * @param bool $isActiveOnlyFlag * @return array */ public function aroundToOptionArray(\Magento\Shipping\Model\Config\Source\Allmethods $subject, \Closure $proceed, $isActiveOnlyFlag = false) { $result = $proceed(); $methods = [['value' => '', 'label' => '']]; $carriers = $this->shippingConfig->getAllCarriers(); foreach ($carriers as $carrierCode => $carrierModel) { if (!$carrierModel->isActive() && (bool) $isActiveOnlyFlag === true || in_array($carrierCode, $this->ignoreCarrierCodes)) { continue; } if (strstr($carrierCode, 'shq') && $carrierModel instanceof \ShipperHQ\Shipper\Model\Carrier\Shipper) { $carrierMethods = $carrierModel->getAllowedMethodsByCode($carrierCode); } else { $carrierMethods = $carrierModel->getAllowedMethods(); } if (!$carrierMethods) { continue; } $carrierTitle = $this->scopeConfig->getValue('carriers/' . $carrierCode . '/title', \Magento\Store\Model\ScopeInterface::SCOPE_STORE); $methods[$carrierCode] = ['label' => $carrierTitle, 'value' => []]; foreach ($carrierMethods as $methodCode => $methodTitle) { $methods[$carrierCode]['value'][] = ['value' => $carrierCode . '_' . $methodCode, 'label' => '[' . $carrierCode . '] ' . $methodTitle]; } } $this->shipperLogger->postDebug('ShipperHQ', 'Modifying shipping all methods response', ''); return $methods; }
/** * Get backup carrier if configured * @return mixed */ protected function retrieveBackupCarrier($backupCarrierDetails) { $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Unable to establish connection with ShipperHQ', 'Attempting to use backup carrier: ' . $backupCarrierDetails); if (!$backupCarrierDetails) { $this->shipperLogger->postDebug('Shipperhq_Shipper', 'Backup carrier: ', 'No backup carrier is configured'); return false; } return $backupCarrierDetails; }
/** *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; }
protected function saveSynchData($data) { $result = 0; try { $this->synchronizeFactory->create()->deleteAllSynchData(); } catch (\Exception $e) { $result = false; $this->shipperLogger->postDebug('Shipperhq_Shipper', 'Unable to remove existing attribute update data', $e->getMessage()); } if (empty($data)) { $this->shipperLogger->postDebug('Shipperhq_Shipper', 'Saving synch data', 'No attribute changes required, 0 rows saved'); return $result; } foreach ($data as $update) { $newUpdate = $this->synchronizeFactory->create(); $newUpdate->setData($update); $newUpdate->save(); $result++; } return $result; }
/** * Do remote request for and handle errors * * @return Mage_Shipping_Model_Rate_Result */ protected function getQuotes() { $requestString = serialize($this->shipperRequest); $resultSet = $this->carrierCache->getCachedQuotes($requestString, $this->getCarrierCode()); $timeout = $this->restHelper->getWebserviceTimeout(); if (!$resultSet) { $initVal = microtime(true); $resultSet = $this->shipperWSClientFactory->create()->sendAndReceive($this->shipperRequest, $this->restHelper->getRateGatewayUrl(), $timeout); $elapsed = microtime(true) - $initVal; $this->shipperLogger->postDebug('Shipperhq_Shipper', 'Short lapse', $elapsed); if (!$resultSet['result']) { $backupRates = $this->backupCarrier->getBackupCarrierRates($this->rawRequest, $this->getConfigData("backup_carrier")); if ($backupRates) { return $backupRates; } } $this->carrierCache->setCachedQuotes($requestString, $resultSet, $this->getCarrierCode()); } $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Rate request and result', $resultSet['debug']); return $this->parseShipperResponse($resultSet['result']); }