config = $config; $this->classNameRepository = $classNameRepository; $this->lineItemFactory = $lineItemFactory; } /** * @inheritdoc */ public function process(RequestInterface $request, OrderInterface $order) { $lineItems = $request->getLineItems(); $extensionAttributes = $order->getExtensionAttributes(); if ($extensionAttributes === null || !$extensionAttributes instanceof OrderExtensionInterface) { return $request; } /** @var ShippingAssignmentInterface[]|null $shippingAssignments */ $shippingAssignments = $extensionAttributes->getShippingAssignments(); if ($shippingAssignments === null) { return $request; } // Pre-fetch the shipping tax class since all shipment types have the same one $taxClassId = $this->config->getShippingTaxClassId($order->getStoreId()); $productClass = $this->classNameRepository->getById($taxClassId); foreach ($shippingAssignments as $shippingAssignment) { // This just gathers those variables $shipping = $shippingAssignment->getShipping(); if ($shipping === null || !$shipping instanceof ShippingInterface) { continue; } $total = $shipping->getTotal(); if ($total === null || !$total instanceof TotalInterface) { continue; } $cost = $total->getBaseShippingAmount() - $total->getBaseShippingDiscountAmount(); $lineItem = $this->lineItemFactory->create(); $lineItem->setProductCode($shipping->getMethod()); $lineItem->setProductClass($productClass); $lineItem->setUnitPrice($cost); $lineItem->setQuantity(1); $lineItem->setExtendedPrice($cost); $lineItems[] = $lineItem; } $request->setLineItems($lineItems); return $request; } }