shipmentFactory = $shipmentFactory; $this->trackFactory = $trackFactory; $this->hydratorPool = $hydratorPool; $this->extensionAttributesProcessor = $extensionAttributesProcessor ?: ObjectManager::getInstance() ->get(ExtensionAttributesProcessor::class); } /** * @SuppressWarnings(PHPMD.UnusedFormalParameter) * * @param OrderInterface $order * @param ShipmentItemCreationInterface[] $items * @param ShipmentTrackCreationInterface[] $tracks * @param ShipmentCommentCreationInterface|null $comment * @param bool $appendComment * @param ShipmentPackageCreationInterface[] $packages * @param ShipmentCreationArgumentsInterface|null $arguments * @return ShipmentInterface * @since 100.1.2 */ public function create( OrderInterface $order, array $items = [], array $tracks = [], ShipmentCommentCreationInterface $comment = null, $appendComment = false, array $packages = [], ShipmentCreationArgumentsInterface $arguments = null ) { $shipmentItems = empty($items) ? $this->getQuantitiesFromOrderItems($order->getItems()) : $this->getQuantitiesFromShipmentItems($items); /** @var Shipment $shipment */ $shipment = $this->shipmentFactory->create( $order, $shipmentItems ); if (null !== $arguments) { $this->extensionAttributesProcessor->execute($shipment, $arguments); } foreach ($tracks as $track) { $hydrator = $this->hydratorPool->getHydrator( \Magento\Sales\Api\Data\ShipmentTrackCreationInterface::class ); $shipment->addTrack($this->trackFactory->create(['data' => $hydrator->extract($track)])); } if ($comment) { $shipment->addComment( $comment->getComment(), $appendComment, $comment->getIsVisibleOnFront() ); if ($appendComment) { $shipment->setCustomerNote($comment->getComment()); $shipment->setCustomerNoteNotify($appendComment); } } return $shipment; } /** * Translate OrderItemInterface array to product id => product quantity array. * * @param OrderItemInterface[] $items * @return int[] */ private function getQuantitiesFromOrderItems(array $items) { $shipmentItems = []; foreach ($items as $item) { if (!$item->getIsVirtual() && (!$item->getParentItem() || $item->isShipSeparately())) { $shipmentItems[$item->getItemId()] = $item->getQtyOrdered(); } } return $shipmentItems; } /** * Translate ShipmentItemCreationInterface array to product id => product quantity array. * * @param ShipmentItemCreationInterface[] $items * @return int[] */ private function getQuantitiesFromShipmentItems(array $items) { $shipmentItems = []; foreach ($items as $item) { $shipmentItems[$item->getOrderItemId()] = $item->getQty(); } return $shipmentItems; } }