/** * Creates shipment instance with specified parameters. * * @param \Magento\Sales\Model\Order $order * @param array $items * @param array|null $tracks * @return \Magento\Sales\Api\Data\ShipmentInterface */ public function create(\Magento\Sales\Model\Order $order, array $items = [], $tracks = null) { $shipment = $this->prepareItems($this->converter->toShipment($order), $order, $items); if ($tracks) { $shipment = $this->prepareTracks($shipment, $tracks); } return $shipment; }
/** * Prepare order shipment based on order items and requested items qty * * @param array $qtys * @return \Magento\Sales\Model\Order\Shipment */ public function prepareShipment($qtys = []) { $totalQty = 0; $shipment = $this->_convertor->toShipment($this->_order); foreach ($this->_order->getAllItems() as $orderItem) { if (!$this->_canShipItem($orderItem, $qtys)) { continue; } $item = $this->_convertor->itemToShipmentItem($orderItem); if ($orderItem->isDummy(true)) { $qty = 0; if (isset($qtys[$orderItem->getParentItemId()])) { $productOptions = $orderItem->getProductOptions(); if (isset($productOptions['bundle_selection_attributes'])) { $bundleSelectionAttributes = unserialize($productOptions['bundle_selection_attributes']); if ($bundleSelectionAttributes) { $qty = $bundleSelectionAttributes['qty'] * $qtys[$orderItem->getParentItemId()]; $qty = min($qty, $orderItem->getSimpleQtyToShip()); $item->setQty($qty); $shipment->addItem($item); continue; } else { $qty = 1; } } } else { $qty = 1; } } else { if (isset($qtys[$orderItem->getId()])) { $qty = min($qtys[$orderItem->getId()], $orderItem->getQtyToShip()); } elseif (!count($qtys)) { $qty = $orderItem->getQtyToShip(); } else { continue; } } $totalQty += $qty; $item->setQty($qty); $shipment->addItem($item); } $shipment->setTotalQty($totalQty); return $shipment; }