How to Create Shipment Programmatically in Magento 2

Here are some steps which help to Create Shipment Programmatically in Magento 2.

If we want to create shipment for an order then you can form by following method.For automation magento we need auto create a shipment, so i choose to write this post for create shipment by code.

The following are the steps which used to create a shipment after placing an order it will auto generate shipment:

// Load the order

$order = $this->_objectManager->create('Magento\Sales\Model\Order')

    ->loadByAttribute('increment_id', '000000001');


$order = $this->_objectManager->create('Magento\Sales\Model\Order')


// Check if order can be shipped or has already shipped

if (! $order->canShip()) {

    throw new \Magento\Framework\Exception\LocalizedException(

                    __('You can\'t create an shipment.')



// Initialize the order shipment object

$convertOrder = $this->_objectManager->create('Magento\Sales\Model\Convert\Order');

$shipment = $convertOrder->toShipment($order);

// Loop through order items

foreach ($order->getAllItems() AS $orderItem) {

    // Check if order item has qty to ship or is virtual

    if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {



 $qtyShipped = $orderItem->getQtyToShip();

// Create shipment item with qty

    $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);

// Add shipment item to shipment



// Register shipment



try {

    // Save created shipment and order



 // Send email




} catch (\Exception $e) {

    throw new \Magento\Framework\Exception\LocalizedException(




I hope the following steps help you to Create Shipment Programmatically in Magento 2

Last Update: April 9, 2018  

February 8, 2018   522   Nandini Ramachandran    Shipping    
Total 0 Votes:

Tell us how can we improve this post?

+ = Verify Human or Spambot ?

Leave a Reply

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