In Magento 2, creating shipment programmatically is a very easy and convenient way to add the shipments as required. Here is code you might need to run on Magento 2 console to get it done. All you might require to do is pass order id in generateShipment($orderId) function from the template.
The below code snippet is used for generating Shipment in Magento 2, once the shipment is generated, send the shipment mail to a customer who has placed the order automatically.
<?php namespace MageDelight\Shipment\Block; class Shipment extends \Magento\Framework\View\Element\Template { public function __construct( \Magento\Framework\View\Element\Template\Context $context, \Magento\Sales\Api\OrderRepositoryInterface $orderRepository, \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory, \Magento\Framework\DB\TransactionFactory $transactionFactory, \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender, \Magento\Framework\Message\ManagerInterface $messageManager, array $data = [] ) { $this->orderRepository = $orderRepository; $this->orderConverter = $convertOrderFactory->create(); $this->transactionFactory = $transactionFactory; $this->messageManager = $messageManager; $this->shipmentSender = $shipmentSender; parent::__construct($context, $data); } /** * Create Shipment Based on Order Object * @param \Magento\Sales\Model\Order $order * @return $this */ public function generateShipment($orderId) { try { $order = $this->orderRepository->get($orderId); if (!$order->getId()) { throw new \Magento\Framework\Exception\LocalizedException(__('The order no longer exists.')); } /* check shipment exist for order or not */ if ($order->canShip()) { // Initialize the order shipment object $shipment = $this->orderConverter->toShipment($order); foreach ($order->getAllItems() AS $orderItem) { // Check if order item has qty to ship or is order is virtual if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) { continue; } $qtyShipped = $orderItem->getQtyToShip(); // Create shipment item with qty $shipmentItem = $this->orderConverter->itemToShipmentItem($orderItem)->setQty($qtyShipped); // Add shipment item to shipment $shipment->addItem($shipmentItem); } // Register shipment $shipment->register(); $shipment->getOrder()->setIsInProcess(true); try { $transaction = $this->transactionFactory->create()->addObject($shipment) ->addObject($shipment->getOrder()) ->save(); echo $shipmentId = $shipment->getIncrementId(); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t generate shipment.')); } if ($shipment) { try { $this->shipmentSender->send($shipment); } catch (\Exception $e) { $this->messageManager->addError(__('We can\'t send the shipment right now.')); } } return $shipmentId; } } catch (\Exception $e) { $this->messageManager->addError($e->getMessage()); } return true; }
That's it!
Your store now has a new shipment. We at MageDelight recommend you to create the shipment programmatically. Hope this article has helped you with everything you might need to create shipment programmatically. If we missed out anything, feel free to reach us out or mention it in the comment section.
Also read: How to Get Shipment Tracking Information Programmatically in Magento 2?