shippingMethodDataFactory = $shippingMethodDataFactory; $this->storeManager = $storeManager; $this->taxHelper = $taxHelper; } /** * 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 \Magento\Quote\Api\Data\ShippingMethodInterface 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)) ->setErrorMessage(empty($errorMessage) ? false : $errorMessage) ->setPriceExclTax( $currency->convert($this->getShippingPriceWithFlag($rateModel, false), $quoteCurrencyCode) ) ->setPriceInclTax( $currency->convert($this->getShippingPriceWithFlag($rateModel, true), $quoteCurrencyCode) ); } /** * Get Shipping Price including or excluding tax * * @param \Magento\Quote\Model\Quote\Address\Rate $rateModel * @param bool $flag * @return float */ private function getShippingPriceWithFlag($rateModel, $flag) { return $this->taxHelper->getShippingPrice( $rateModel->getPrice(), $flag, $rateModel->getAddress(), $rateModel->getAddress()->getQuote()->getCustomerTaxClassId() ); } }