14 use Magento\Sales\Model\Order\Shipment\TrackFactory;
32 private $shipmentFactory;
37 private $trackFactory;
42 private $hydratorPool;
47 private $extensionAttributesProcessor;
60 TrackFactory $trackFactory,
63 $this->shipmentFactory = $shipmentFactory;
64 $this->trackFactory = $trackFactory;
65 $this->hydratorPool = $hydratorPool;
67 ->get(ExtensionAttributesProcessor::class);
83 public function create(
88 $appendComment =
false,
92 $shipmentItems = empty(
$items)
93 ? $this->getQuantitiesFromOrderItems(
$order->getItems())
94 : $this->getQuantitiesFromShipmentItems(
$items);
97 $shipment = $this->shipmentFactory->create(
107 $hydrator = $this->hydratorPool->getHydrator(
108 \
Magento\Sales\Api\
Data\ShipmentTrackCreationInterface::class
110 $shipment->addTrack($this->trackFactory->create([
'data' => $hydrator->extract(
$track)]));
115 $comment->getComment(),
117 $comment->getIsVisibleOnFront()
120 if ($appendComment) {
121 $shipment->setCustomerNote($comment->getComment());
122 $shipment->setCustomerNoteNotify($appendComment);
135 private function getQuantitiesFromOrderItems(array
$items)
139 if (!
$item->getIsVirtual() && (!
$item->getParentItem() ||
$item->isShipSeparately())) {
140 $shipmentItems[
$item->getItemId()] =
$item->getQtyOrdered();
143 return $shipmentItems;
152 private function getQuantitiesFromShipmentItems(array
$items)
156 $shipmentItems[
$item->getOrderItemId()] =
$item->getQty();
158 return $shipmentItems;
__construct(ShipmentFactory $shipmentFactory, HydratorPool $hydratorPool, TrackFactory $trackFactory, ExtensionAttributesProcessor $extensionAttributesProcessor=null)
foreach($order->getItems() as $orderItem) $shipment