/** * Converts a specified rate model to a shipping method data object. * * @param string $quoteCurrencyCode The quote currency code. * @param \Magento\Quote\Model\Quote\Address\Rate $rateModel The rate model. * @return mixed Shipping method data object. */ public function modelToDataObject($rateModel, $quoteCurrencyCode) { /** @var \Magento\Directory\Model\Currency $currency */ $currency = $this->storeManager->getStore()->getBaseCurrency(); $errorMessage = $rateModel->getErrorMessage(); return $this->shippingMethodDataFactory->create()->setCarrierCode($rateModel->getCarrier())->setMethodCode($rateModel->getMethod())->setCarrierTitle($rateModel->getCarrierTitle())->setMethodTitle($rateModel->getMethodTitle())->setAmount($currency->convert($rateModel->getPrice(), $quoteCurrencyCode))->setBaseAmount($rateModel->getPrice())->setAvailable(empty($errorMessage)); }
/** * {@inheritDoc} */ public function get($cartId) { /** @var \Magento\Quote\Model\Quote $quote */ $quote = $this->quoteRepository->getActive($cartId); /** @var \Magento\Quote\Model\Quote\Address $shippingAddress */ $shippingAddress = $quote->getShippingAddress(); if (!$shippingAddress->getCountryId()) { throw new StateException(__('Shipping address not set.')); } $shippingMethod = $shippingAddress->getShippingMethod(); if (!$shippingMethod) { return null; } list($carrierCode, $methodCode) = $this->divideNames('_', $shippingAddress->getShippingMethod()); list($carrierTitle, $methodTitle) = $this->divideNames(' - ', $shippingAddress->getShippingDescription()); return $this->methodDataFactory->create()->setCarrierCode($carrierCode)->setMethodCode($methodCode)->setCarrierTitle($carrierTitle)->setMethodTitle($methodTitle)->setAmount($shippingAddress->getShippingAmount())->setBaseAmount($shippingAddress->getBaseShippingAmount())->setAvailable(true); }