/** * @param RateRequest $request * @return \Magento\Shipping\Model\Rate\Result */ public function collectRates(RateRequest $request) { if (!$this->getConfigFlag('active')) { return false; } /** @var Result $result */ $result = $this->_rateResultFactory->create(); $packageValue = $request->getBaseCurrency()->convert($request->getPackageValue(), $request->getPackageCurrency()); for ($i = 0; $i <= 5; $i++) { if ($this->getConfigData('type' . $i) == 'O') { // per order $shippingPrice = $this->getConfigData('price' . $i); } elseif ($this->getConfigData('type' . $i) == 'I') { // per item $shippingPrice = $request->getPackageQty() * $this->getConfigData('price' . $i) - $this->getFreeBoxes() * $this->getConfigData('price' . $i); } else { $shippingPrice = $this->getConfigData('price' . $i); } $shippingName = $this->getConfigData('name' . $i); if ($shippingName != "" && ($packageValue >= $this->getConfigData('min_shipping' . $i) && $packageValue <= $this->getConfigData('max_shipping' . $i)) or $shippingName != "" && $this->getConfigData('max_shipping' . $i) == "") { /** @var \Magento\Quote\Model\Quote\Address\RateResult\Method $method */ $method = $this->_rateMethodFactory->create(); $method->setCarrier('msmultiflat'); $method->setCarrierTitle($this->getConfigData('title')); $method->setMethod($this->getConfigData('name' . $i)); $method->setMethodTitle($this->getConfigData('name' . $i)); $method->setMethodDetails($this->getConfigData('details' . $i)); $method->setMethodDescription($this->getConfigData('details' . $i)); $method->setPrice($shippingPrice); $method->setCost($shippingPrice); $result->append($method); } } return $result; }
/** * Prepare shipping rate result based on response * * @param mixed $xmlResponse * @return Result * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function _parseXmlResponse($xmlResponse) { $costArr = []; $priceArr = []; if (strlen(trim($xmlResponse)) > 0) { $xml = new \Magento\Framework\Simplexml\Config(); $xml->loadString($xmlResponse); $arr = $xml->getXpath("//RatingServiceSelectionResponse/Response/ResponseStatusCode/text()"); $success = (int) $arr[0]; if ($success === 1) { $arr = $xml->getXpath("//RatingServiceSelectionResponse/RatedShipment"); $allowedMethods = explode(",", $this->getConfigData('allowed_methods')); // Negotiated rates $negotiatedArr = $xml->getXpath("//RatingServiceSelectionResponse/RatedShipment/NegotiatedRates"); $negotiatedActive = $this->getConfigFlag('negotiated_active') && $this->getConfigData('shipper_number') && !empty($negotiatedArr); $allowedCurrencies = $this->_currencyFactory->create()->getConfigAllowCurrencies(); foreach ($arr as $shipElement) { $code = (string) $shipElement->Service->Code; if (in_array($code, $allowedMethods)) { if ($negotiatedActive) { $cost = $shipElement->NegotiatedRates->NetSummaryCharges->GrandTotal->MonetaryValue; } else { $cost = $shipElement->TotalCharges->MonetaryValue; } //convert price with Origin country currency code to base currency code $successConversion = true; $responseCurrencyCode = (string) $shipElement->TotalCharges->CurrencyCode; if ($responseCurrencyCode) { if (in_array($responseCurrencyCode, $allowedCurrencies)) { $cost = (double) $cost * $this->_getBaseCurrencyRate($responseCurrencyCode); } else { $errorTitle = __('We can\'t convert a rate from "%1-%2".', $responseCurrencyCode, $this->_request->getPackageCurrency()->getCode()); $error = $this->_rateErrorFactory->create(); $error->setCarrier('ups'); $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($errorTitle); $successConversion = false; } } if ($successConversion) { $costArr[$code] = $cost; $priceArr[$code] = $this->getMethodPrice(floatval($cost), $code); } } } } else { $arr = $xml->getXpath("//RatingServiceSelectionResponse/Response/Error/ErrorDescription/text()"); $errorTitle = (string) $arr[0][0]; $error = $this->_rateErrorFactory->create(); $error->setCarrier('ups'); $error->setCarrierTitle($this->getConfigData('title')); $error->setErrorMessage($this->getConfigData('specificerrmsg')); } } $result = $this->_rateFactory->create(); if (empty($priceArr)) { $error = $this->_rateErrorFactory->create(); $error->setCarrier('ups'); $error->setCarrierTitle($this->getConfigData('title')); if (!isset($errorTitle)) { $errorTitle = __('Cannot retrieve shipping rates'); } $error->setErrorMessage($this->getConfigData('specificerrmsg')); $result->append($error); } else { foreach ($priceArr as $method => $price) { $rate = $this->_rateMethodFactory->create(); $rate->setCarrier('ups'); $rate->setCarrierTitle($this->getConfigData('title')); $rate->setMethod($method); $methodArr = $this->getShipmentByCode($method); $rate->setMethodTitle($methodArr); $rate->setCost($costArr[$method]); $rate->setPrice($price); $result->append($rate); } } return $result; }