13 use Magento\Sales\Api\Data\ShipmentTrackCreationInterfaceFactory;
14 use Magento\Sales\Api\Data\ShipmentItemCreationInterfaceFactory;
39 private $messageManager;
49 private $shipmentRepository;
54 private $orderRepository;
59 private $documentFactory;
64 private $trackFactory;
87 ShipmentTrackCreationInterfaceFactory $trackFactory,
88 ShipmentItemCreationInterfaceFactory $itemFactory,
91 $this->messageManager = $messageManager;
93 $this->shipmentRepository = $shipmentRepository;
95 $this->documentFactory = $documentFactory;
96 $this->trackFactory = $trackFactory;
98 parent::__construct(
$data);
110 $orderId = $this->getOrderId();
111 $shipmentId = $this->getShipmentId();
113 $shipment = $this->shipmentRepository->get($shipmentId);
115 $order = $this->orderRepository->get($orderId);
121 $this->messageManager->addError(
__(
'The order no longer exists.'));
127 if (
$order->getForcedShipmentWithInvoice()) {
128 $this->messageManager->addError(
__(
'Cannot do shipment for the order separately from invoice.'));
135 $this->messageManager->addError(
__(
'Cannot do shipment for the order.'));
139 $shipmentItems = $this->getShipmentItems($this->
getShipment());
141 $shipment = $this->documentFactory->create(
144 $this->getTrackingArray()
148 $this->registry->register(
'current_shipment',
$shipment);
158 private function getTrackingArray()
160 $tracks = $this->getTracking() ?: [];
161 $trackingCreation = [];
163 if (!isset(
$track[
'number']) || !isset(
$track[
'title']) || !isset(
$track[
'carrier_code'])) {
165 __(
'Tracking information must contain title, carrier code, and tracking number')
169 $trackCreation = $this->trackFactory->create();
170 $trackCreation->setTrackNumber(
$track[
'number']);
171 $trackCreation->setTitle(
$track[
'title']);
172 $trackCreation->setCarrierCode(
$track[
'carrier_code']);
173 $trackingCreation[] = $trackCreation;
176 return $trackingCreation;
185 private function getShipmentItems(array $shipmentData)
188 $itemQty = isset($shipmentData[
'items']) ? $shipmentData[
'items'] : [];
189 foreach ($itemQty as $itemId => $quantity) {
191 $item = $this->itemFactory->create();
192 $item->setOrderItemId($itemId);
193 $item->setQty($quantity);
194 $shipmentItems[] =
$item;
196 return $shipmentItems;
206 return $this->
getData(self::SHIPMENT) ?: [];
getData($key='', $index=null)
elseif(isset( $params[ 'redirect_parent']))
__construct(ManagerInterface $messageManager, Registry $registry, ShipmentRepositoryInterface $shipmentRepository, OrderRepositoryInterface $orderRepository, ShipmentDocumentFactory $documentFactory, ShipmentTrackCreationInterfaceFactory $trackFactory, ShipmentItemCreationInterfaceFactory $itemFactory, array $data=[])
foreach($order->getItems() as $orderItem) $shipment