---
title: "How to Create Shipment Programmatically in Magento 2"
url: "https://magecomp.com/blog/create-shipment-programmatically-magento-2/"
date: "2018-05-14T17:33:17+00:00"
modified: "2025-06-05T12:31:02+00:00"
author:
  name: "Dhiren Vasoya"
  url: "https://magecomp.com/"
categories:
  - "How To"
  - "Magento 2"
word_count: 405
reading_time: "3 min read"
summary: "Over the years, online shopping has gained fame and due to that nowadays if someone wants to buy a product, instead of walking into the store people has started ordering products online with just a..."
description: "Once the order is placed it passes through various order states and statuses and shipping is one of them. Creating a shipment for an order doesn't take a lon..."
keywords: "How To, Magento 2"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Get Order Information Using SOAP API in Magento 2"
    url: "https://magecomp.com/blog/get-order-information-using-soap-api-magento-2/"
  - title: "How to Restrict Payment Method Based on Shipping selection in Magento 2"
    url: "https://magecomp.com/blog/restrict-payment-method-based-on-shipping-selection-in-magento-2/"
  - title: "How to Implement Multiselect Filter for Order Status in Magento 2 Order Grid"
    url: "https://magecomp.com/blog/how-to-implement-multiselect-filter-for-order-status-in-magento-2-order-grid/"
---

# How to Create Shipment Programmatically in Magento 2

_Published: May 14, 2018_  
_Author: Dhiren Vasoya_  

![How to create shipment programmatically in magento 2 via script](https://magecomp.com/blog/wp-content/uploads/2018/05/How-to-create-shipment-programmatically-in-magento-2-via-script-1024x332.webp)

Over the years, online shopping has gained fame and due to that nowadays if someone wants to buy a product, instead of walking into the store people has started ordering products online with just a few clicks. To run a successful online E-commerce business there are several CMS available to handle sales and product easily by reducing efforts of store owners. Once the order is placed it passes through various order states and statuses and shipping is one of them. Creating a shipment for an order doesn’t take a long but when there is a large number of orders it becomes a time-consuming task for store owners.

[![Magento Malware Removal](https://magecomp.com/blog/wp-content/uploads/2023/04/Magento-Malware-Removal-CTA-1.webp)](https://magecomp.com/magento-malware-removal.html)

Recently we need to generate shipment for multiple orders and to save time and efforts we created a script to generate shipment programmatically in Magento 2 and it worked because it is useful to everyone we decided to share on our MageComp blog.

[![Magento 2 Speed & Performance Optimization Services](https://magecomp.com/blog/wp-content/uploads/2025/02/Magento-2-Speed-Performance-Optimization-Services-CTA-1.webp)](https://magecomp.com/magento-performance-optimization.html)

All you need to do is simply upload this script on the root folder of your server and hit the URL of script followed by the filename.

Create a file in your Magento root directory at the below path

**magento_root_directory\shipmentcreate.php**

```
<?php <br></br>use MagentoFrameworkAppBootstrap; <br></br>include('app/bootstrap.php'); <br></br>$bootstrap = Bootstrap::create(BP, $_SERVER); <br></br>$objectManager = $bootstrap->getObjectManager();<br></br>$state = $objectManager->get('MagentoFrameworkAppState');<br></br>$state->setAreaCode('frontend');<br></br><br></br>$orderInterface = $objectManager->get('MagentoSalesApiDataOrderInterface');<br></br><br></br>//Use this if you have orderId<br></br>//$orderId = "100"; //Order Id<br></br>//$order = $orderInterface->load($orderId);<br></br>$incrementId = "000000165"; //Increment Id<br></br>$order = $objectManager->create('MagentoSalesModelOrder')->loadByAttribute('increment_id', $incrementId);<br></br>if ($order->canShip()) {<br></br>    // Initialize the order shipment object<br></br>    $convertOrder = $objectManager->create('MagentoSalesModelConvertOrder');<br></br>    $shipment = $convertOrder->toShipment($order);<br></br>    // Loop through order items<br></br>    foreach ($order->getAllItems() AS $orderItem) {<br></br>        // Check if order item has qty to ship or is virtual<br></br>        if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual())     {<br></br>            continue;<br></br>        }<br></br>        $qtyShipped = $orderItem->getQtyToShip();<br></br>        // Create shipment item with qty<br></br>        $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);<br></br>        // Add shipment item to shipment<br></br>        $shipment->addItem($shipmentItem);<br></br>    }<br></br>    // Register shipment<br></br>    $shipment->register();<br></br>    $shipment->getOrder()->setIsInProcess(true);<br></br>    try {<br></br>        // Save created shipment and order<br></br>        $shipment->save();<br></br>        $shipment->getOrder()->save();<br></br><br></br>    } catch (Exception $e) {<br></br>        echo "Shipment Not Created". $e->getMessage(); exit;<br></br>    }<br></br>    echo "Shipment Succesfully Generated for order: #".$incrementId;<br></br>} else {<br></br>    echo "Shipment Not Created Because It's already created or something went wrong";<br></br>}
```

 Happy Coding!

[![hyva theme development](https://magecomp.com/blog/wp-content/uploads/2025/02/hyva-theme-development-CTA-1.webp)](https://magecomp.com/services/hyva-theme-development/)


---

_View the original post at: [https://magecomp.com/blog/create-shipment-programmatically-magento-2/](https://magecomp.com/blog/create-shipment-programmatically-magento-2/)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-20 21:01:33 UTC_  
