config = $config; $this->taxClassNameRepository = $taxClassNameRepository; $this->lineItemFactory = $lineItemFactory; } /** * Create properly formatted Line Item data for the Order Shipping * * @param AddressInterface $taxAddress * @param string|null $scopeCode * @return LineItemInterface */ public function getFormattedShippingLineItemData(AddressInterface $taxAddress, $scopeCode = null) { /** @var LineItemInterface $item */ $item = $this->lineItemFactory->create(); $item->setProductCode(substr($taxAddress->getShippingMethod(), 0, Config::MAX_CHAR_PRODUCT_CODE_ALLOWED)); $item->setProductClass( substr( $this->taxClassNameRepository->getById( $this->config->getShippingTaxClassId($scopeCode) ), 0, Config::MAX_CHAR_PRODUCT_CODE_ALLOWED ) ); $item->setQuantity(1); $rate = $taxAddress->getShippingRateByCode($taxAddress->getShippingMethod()); if (!$rate && $taxAddress->getShippingMethod()) { $taxAddress->setCollectShippingRates(true)->collectShippingRates(); } foreach ($taxAddress->getAllShippingRates() as $rateCandidate) { if ($rateCandidate->getCode() === $taxAddress->getShippingMethod()) { $rate = $rateCandidate; break; } } $unitPrice = $rate ? $rate->getPrice() : 0; $extendedPrice = $unitPrice ? $unitPrice - $taxAddress->getBaseShippingDiscountAmount() : 0; $item->setUnitPrice($unitPrice); $item->setExtendedPrice($extendedPrice); $item->setLineItemId(static::LINE_ITEM_ID); return $item; } }