How to Create Shipment Programmatically in Magento 2

In Magento 2, creating a shipment programmatically is the convenient way to add the new shipment as you need. If we need to programmatically create shipment for an order then you can create by following way.

Use the following code to create shipment programmatically for an order in Magento 2:

// Loading 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 has already shipping or can be shipped
if (! $order->canShip()) {
throw new \Magento\Framework\Exception\LocalizedException(
__('You cant create the Shipment.') );
// Initializzing Object for the order shipment
$convertOrder = $this->_objectManager->create('Magento\Sales\Model\Convert\Order');
$shipment = $convertOrder->toShipment($order);
// Looping the Order Items
foreach ($order->getAllItems() AS $orderItem) {
// Check if the order item has Quantity to ship or is virtual
if (! $orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {
$qtyShipped = $orderItem->getQtyToShip();
// Create Shipment Item with Quantity
$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(

Let me know if you have any question or faced any issue while following this method for creating Shipment Programmatically in Magento 2.

You May Also Like

Editorial Staff

About the Author: Editorial Staff

Editorial Staff at the MageBeginners is a team of Magento experts writing about Magento Tutorials, Themes, Extensions, Guides, Tricks and much more...

Leave a Reply

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