protected function updateAll($updateData) { $result = 0; foreach ($updateData as $attributeUpdate) { if ($attributeUpdate['attribute_type'] == 'product') { if ($attributeUpdate['status'] == self::ADD_ATTRIBUTE_OPTION) { $optionToAdd = $this->optionDataFactory->create(); $optionToAdd->setLabel($attributeUpdate['value'])->setSortOrder(0)->setIsDefault(0); try { $this->attributeOptionManagement->add($attributeUpdate['attribute_code'], $optionToAdd); $result++; } catch (\Exception $e) { $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Unable to add attribute option', 'Error: ' . $e->getMessage()); $result = false; } } else { if ($attributeUpdate['status'] == self::AUTO_REMOVE_ATTRIBUTE_OPTION) { try { $this->attributeOptionManagement->delete($attributeUpdate['attribute_code'], $attributeUpdate['option_id']); $result++; } catch (\Exception $e) { $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Unable to remove attribute option', 'Error: ' . $e->getMessage()); $result = false; } } } } elseif ($attributeUpdate['attribute_type'] == 'global_setting') { $this->carrierConfigHandler->saveConfig('carriers/shipper/' . $attributeUpdate['attribute_code'], $attributeUpdate['value']); } } if ($result >= 0) { $this->checkSynchStatus(true); } return $result; }
/** * 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; }
protected function recordOrder($order) { $customOrderId = null; //https://github.com/magento/magento2/issues/4233 $quoteId = $order->getQuoteId(); $quote = $this->quoteRepository->get($quoteId); $shippingAddress = $quote->getShippingAddress(); $carrierType = $shippingAddress->getCarrierType(); // $order->setCarrierType($carrierType); $order->setDestinationType($shippingAddress->getDestinationType()); $order->setValidationStatus($shippingAddress->getValidationStatus()); $this->carrierGroupHelper->saveOrderDetail($order, $shippingAddress); $this->carrierGroupHelper->recordOrderItems($order); $this->packageHelper->saveOrderPackages($order, $shippingAddress); if (strstr($order->getShippingMethod(), 'shqshared_')) { $orderDetailArray = $this->carrierGroupHelper->loadOrderDetailByOrderId($order->getId()); //SHQ16- Review for splits foreach ($orderDetailArray as $orderDetail) { $original = $orderDetail->getCarrierType(); $carrierTypeArray = explode('_', $orderDetail->getCarrierType()); if (is_array($carrierTypeArray)) { $orderDetail->setCarrierType($carrierTypeArray[1]); //SHQ16-1026 $currentShipDescription = $order->getShippingDescription(); $shipDescriptionArray = explode('-', $currentShipDescription); $cgArray = $this->shipperDataHelper->decodeShippingDetails($orderDetail->getCarrierGroupDetail()); foreach ($cgArray as $key => $cgDetail) { if (isset($cgDetail['carrierType']) && $cgDetail['carrierType'] == $original) { $cgDetail['carrierType'] = $carrierTypeArray[1]; } if (is_array($shipDescriptionArray) && isset($cgDetail['carrierTitle'])) { $shipDescriptionArray[0] = $cgDetail['carrierTitle'] . ' '; $newShipDescription = implode('-', $shipDescriptionArray); $order->setShippingDescription($newShipDescription); } $cgArray[$key] = $cgDetail; } $encoded = $this->shipperDataHelper->encode($cgArray); $orderDetail->setCarrierGroupDetail($encoded); $orderDetail->save(); } $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Rates displayed as single carrier', 'Resetting carrier type on order to be ' . $carrierTypeArray[1]); } } if ($this->shipperDataHelper->useDefaultCarrierCodes()) { $order->setShippingMethod($this->getDefaultCarrierShipMethod($order, $shippingAddress)); } $order->save(); }
protected function saveShippingAddress($cartId) { $quote = $this->quoteRepository->getActive($cartId); $address = $quote->getShippingAddress(); $region = $address->getRegion(); if (!is_null($region) && $region instanceof \Magento\Customer\Model\Data\Region) { $regionString = $region->getRegion(); $address->setRegion($regionString); } try { $address->save(); } catch (\Exception $e) { $this->shipperLogger->postCritical('Shipperhq_Shipper', 'Exception raised whilst saving shipping address', $e->getMessage()); } }
/** *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; }
/** * * Generate error message from ShipperHQ response. * Display of error messages per carrier is managed in SHQ configuration * * @param $result * @param $errorDetails * @return Mage_Shipping_Model_Rate_Result */ protected function appendError($result, $errorDetails, $carrierCode, $carrierTitle, $carrierGroupId = null, $carrierGroupDetail = null) { if (is_object($errorDetails)) { $errorDetails = get_object_vars($errorDetails); } if (array_key_exists('internalErrorMessage', $errorDetails) && $errorDetails['internalErrorMessage'] != '' || array_key_exists('externalErrorMessage', $errorDetails) && $errorDetails['externalErrorMessage'] != '') { $errorMessage = false; if ($this->getConfigData("debug") && array_key_exists('internalErrorMessage', $errorDetails) && $errorDetails['internalErrorMessage'] != '') { $errorMessage = $errorDetails['internalErrorMessage']; } else { if (array_key_exists('externalErrorMessage', $errorDetails) && $errorDetails['externalErrorMessage'] != '') { $errorMessage = $errorDetails['externalErrorMessage']; } } if (array_key_exists('externalErrorMessage', $errorDetails) && $errorDetails['externalErrorMessage'] != '') { $errorMessage = $errorDetails['externalErrorMessage']; } if ($errorMessage) { $error = $this->_rateErrorFactory->create(); $error->setCarrier($carrierCode); $error->setCarrierTitle($carrierTitle); $error->setErrorMessage($errorMessage); if (!is_null($carrierGroupId)) { $error->setCarriergroupId($carrierGroupId); } if (is_array($carrierGroupDetail) && array_key_exists('checkoutDescription', $carrierGroupDetail)) { $error->setCarriergroup($carrierGroupDetail['checkoutDescription']); } $result->append($error); $this->shipperLogger->postInfo('Shipperhq_Shipper', 'Shipper HQ returned error', $errorDetails); } } return $result; }
/** * Get values for items * * @param $request * @param $magentoItems * @param bool $childItems * @return array */ private function getFormattedItems($request, $magentoItems, $childItems = false) { $formattedItems = []; if (empty($magentoItems)) { return $formattedItems; } $selectedCarriergroupId = false; if ($request->getCarriergroupId() != '') { $selectedCarriergroupId = $request->getCarriergroupId(); } foreach ($magentoItems as $magentoItem) { if (!$childItems && $magentoItem->getParentItemId()) { continue; } //strip out items not required in carriergroup specific request if ($selectedCarriergroupId && $magentoItem->getCarriergroupId() != $selectedCarriergroupId) { continue; } // TODO Excluded from first release //$taxRequest = $this->taxCalculation->getRateOriginRequest(); //$taxRequest->setProductClassId($magentoItem->getProduct()->getTaxClassId()); //$taxPercentage = $this->taxCalculation->getRate($taxRequest); $taxPercentage = 0; // Not in first release of M2 $fixedPrice = $magentoItem->getProduct()->getPriceType() == \Magento\Bundle\Model\Product\Price::PRICE_TYPE_FIXED; $fixedWeight = $magentoItem->getProduct()->getWeightType() == 1 ? true : false; $id = $magentoItem->getItemId() ? $magentoItem->getItemId() : $magentoItem->getQuoteItemId(); $productType = $magentoItem->getProductType() ? $magentoItem->getProductType() : $magentoItem->getProduct()->getTypeId(); $stdAttributes = array_merge($this->getDimensionalAttributes($magentoItem), self::$stdAttributeNames); $options = self::populateCustomOptions($magentoItem); $weight = $magentoItem->getWeight(); if (is_null($weight)) { //SHIPPERHQ-1855 if ($productType != \Magento\Catalog\Model\Product\Type::TYPE_VIRTUAL && $productType != \Magento\Downloadable\Model\Product\Type::TYPE_DOWNLOADABLE) { $this->shipperLogger->postCritical('ShipperHQ', 'Item weight is null, using 0', 'Please review the product configuration for Sku ' . $magentoItem->getSku() . ' as product has NULL weight'); } $weight = 0; } $warehouseDetails = $this->getWarehouseDetails($magentoItem); $pickupLocationDetails = $this->getPickupLocationDetails($magentoItem); $formattedItem = $this->itemFactory->create(['id' => $id, 'sku' => $magentoItem->getSku(), 'qty' => $magentoItem->getQty() ? floatval($magentoItem->getQty()) : 0, 'weight' => $weight, 'rowTotal' => $magentoItem->getRowTotal(), 'basePrice' => $magentoItem->getBasePrice(), 'baseRowTotal' => $magentoItem->getBaseRowTotal(), 'discountAmount' => $magentoItem->getDiscountAmount(), 'discountPercent' => $magentoItem->getDiscountPercent(), 'discountedBasePrice' => $magentoItem->getBasePrice() - $magentoItem->getBaseDiscountAmount() / $magentoItem->getQty(), 'discountedStorePrice' => $magentoItem->getPrice() - $magentoItem->getDiscountAmount() / $magentoItem->getQty(), 'discountedTaxInclBasePrice' => $magentoItem->getBasePrice() - $magentoItem->getBaseDiscountAmount() / $magentoItem->getQty() + $magentoItem->getBaseTaxAmount() / $magentoItem->getQty(), 'discountedTaxInclStorePrice' => $magentoItem->getPrice() - $magentoItem->getDiscountAmount() / $magentoItem->getQty() + $magentoItem->getTaxAmount() / $magentoItem->getQty(), 'fixedPrice' => $fixedPrice, 'fixedWeight' => $fixedWeight, 'freeShipping' => (bool) $magentoItem->getFreeShipping(), 'packageCurrency' => $request->getPackageCurrency()->getCurrencyCode(), 'baseCurrency' => $request->getBaseCurrency()->getCurrencyCode(), 'storeBaseCurrency' => $this->storeManager->getStore()->getBaseCurrencyCode(), 'storeCurrentCurrency' => $this->storeManager->getStore()->getCurrentCurrencyCode(), 'storePrice' => $magentoItem->getPrice() ? $magentoItem->getPrice() : 0, 'taxInclBasePrice' => $magentoItem->getBasePriceInclTax() ? $magentoItem->getBasePriceInclTax() : 0, 'taxInclStorePrice' => $magentoItem->getPriceInclTax() ? $magentoItem->getPriceInclTax() : 0, 'taxPercentage' => $taxPercentage, 'type' => $productType, 'items' => [], 'attributes' => $options ? array_merge(self::populateAttributes($stdAttributes, $magentoItem), $options) : self::populateAttributes($stdAttributes, $magentoItem), 'additionalAttributes' => self::getCustomAttributes($magentoItem), 'warehouseDetails' => $warehouseDetails, 'pickupLocationDetails' => $pickupLocationDetails]); if (count($warehouseDetails) == 0) { $formattedItem->setDefaultWarehouseStockDetail($this->getDefaultWarehouseStockDetail($magentoItem)); } if (!$childItems) { $formattedItem->setItems($this->getFormattedItems($request, $magentoItem->getChildren(), true)); } $formattedItems[] = $formattedItem; } return $formattedItems; }