Menu Close
    Searching for an experienced Magento 2 Development Company ?

    How to programmatically create shipment in magento2

    Here we learn in Magento2- how to create shipment of an order programmatically.
    If you want to create custom quote and order programmatically then you can check this blog Create Quote & Order Programmatically

    Create Shipment of an order-

    <?php
    /**
     * Webkul Software.
     *
     * @category  Webkul
     * @package   Webkul_Test
     * @author    Webkul
     * @copyright Copyright (c) 2010-2018 Webkul Software Private Limited (https://webkul.com)
     * @license   https://store.webkul.com/license.html
     */
     
    namespace Webkul\Test\Controller\Order;
     
    class CreateShipment extends Magento\Framework\App\Action\Action
    {
       /**
         * @var \Magento\Sales\Api\OrderRepositoryInterface
         */
        protected $_orderRepository;
    
        /**
         * @var \Magento\Sales\Model\Convert\Order
         */
        protected $_convertOrder;
    
        /**
         * @var \Magento\Shipping\Model\ShipmentNotifier
         */
        protected $_shipmentNotifier;
    
        /**
         * @param Context                                     $context
         * @param \Magento\Sales\Api\OrderRepositoryInterface $orderRepository
         * @param \Magento\Sales\Model\Convert\Order          $convertOrder
         * @param \Magento\Shipping\Model\ShipmentNotifier    $shipmentNotifier
         */
        public function __construct(
            Context $context,
            \Magento\Sales\Api\OrderRepositoryInterface $orderRepository,
            \Magento\Sales\Model\Convert\Order $convertOrder,
            \Magento\Shipping\Model\ShipmentNotifier $shipmentNotifier
        ) {
            $this->_orderRepository = $orderRepository;
            $this->_convertOrder = $convertOrder;
            $this->_shipmentNotifier = $shipmentNotifier;
            parent::__construct($context);
        }
    
        /**
         * Test Order Create Shipment Controller
         */
        public function execute()
        {
            $orderId = 1;
            $order = $this->_orderRepository->get($orderId);
    
            // to check order can ship or not
            if (!$order->canShip()) {
                throw new \Magento\Framework\Exception\LocalizedException(
                __('You can't create the Shipment of this order.') );
            }
    
            $orderShipment = $this->_convertOrder->toShipment($order);
    
            foreach ($order->getAllItems() AS $orderItem) {
    
             // Check virtual item and item Quantity
             if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
                continue;
             }
    
             $qty = $orderItem->getQtyToShip();
             $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qty);
    
             $orderShipment->addItem($shipmentItem);
            }
    
            $orderShipment->register();
            $orderShipment->getOrder()->setIsInProcess(true);
            try {
    
                // Save created Order Shipment
                $orderShipment->save();
                $orderShipment->getOrder()->save();
    
                // Send Shipment Email
                $this->_shipmentNotifier->notify($orderShipment);
                $orderShipment->save();
            } catch (\Exception $e) {
                throw new \Magento\Framework\Exception\LocalizedException(
                __($e->getMessage())
                );
            }
        }
    }

    In this way, you can create shipment for a particular complete order.
    If you want to create the invoice of an order in Magento 2, you can check the wonderful blog from here Create Invoice Programmatically

    Hope it will help you. Thank you.

     

    . . .
    Discuss on Helpdesk

    Leave a Comment

    Your email address will not be published. Required fields are marked*


    1 comments

  • Marlene Reithmayr
    Thank you for the article, it was very useful. But I have a questions regarding the requirements that order->canShip is true. Is it possible to create a Shipment for an already competed order? If not are there any workarounds?

    Thank you

  • Back to Top