{"id":36,"date":"2017-12-04T14:09:23","date_gmt":"2017-12-04T14:09:23","guid":{"rendered":"https:\/\/meetanshi.com\/blog\/2017\/12\/04\/create-shipment-programmatically-in-magento-2\/"},"modified":"2025-05-22T17:36:28","modified_gmt":"2025-05-22T12:06:28","slug":"create-shipment-programmatically-in-magento-2","status":"publish","type":"post","link":"https:\/\/meetanshi.com\/blog\/create-shipment-programmatically-in-magento-2\/","title":{"rendered":"How to Create Shipment Programmatically in Magento 2"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The method to&nbsp;<em><strong>create shipment programmatically in Magento 2<\/strong><\/em>&nbsp;is a convenient way to add more shipping methods to the Magento 2 store.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/www.statista.com\/statistics\/251666\/number-of-digital-buyers-worldwide\/\" target=\"_blank\" rel=\"noreferrer noopener\">Online shopping is gaining popularity<\/a>&nbsp;and merchants require to fulfill the order with the best facility in order to stand the competition. Shipping is one of them.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Shipping is an important factor in conversion, and these findings from&nbsp;<a href=\"https:\/\/www.statista.com\/study\/45727\/shipping-in-online-retail\/\" target=\"_blank\" rel=\"noreferrer noopener\">Statista<\/a>&nbsp;prove the same:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Shipping terms are an important factor when ordering online \u2013 low acceptance rate for high shipping costs<\/li>\n\n\n\n<li>Even with free shipping, 80% of shoppers expect delivery within seven days at the latest<\/li>\n\n\n\n<li>Shipment tracking and delivery notifications are the most-used services offered by shipping providers and for that you need to&nbsp;<a href=\"https:\/\/meetanshi.com\/blog\/add-tracking-number-to-the-current-order-shipment-in-magento-2\/\">add tracking number to current order shipment in Magento 2.<\/a><\/li>\n\n\n\n<li>Low use of fast shipping and scheduled delivery options; young shoppers use them the most<\/li>\n\n\n\n<li>Younger online shoppers are more likely to use express shipping options to avoid long delivery times<\/li>\n\n\n\n<li>Online shoppers are open to new delivery methods (e.g. via robots and drones)<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/meetanshi.com\/blog\/auto-generate-invoices-and-shipments-for-backend-orders-in-magento-2\/\">Creating shipment in Magento 2<\/a>&nbsp;can be complex, but not with the programmatic method, which will save your time!<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Use the following code to create shipment programmatically for order in Magento 2:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">&lt;?php\n\/\/ Loading the Order\n$order = $this->_objectManager->create('MagentoSalesModelOrder')\n    ->loadByAttribute('increment_id', '000000001');\n\/\/OR\n$order = $this->_objectManager->create('MagentoSalesModelOrder')\n    ->load('1');\n\n\/\/ Check if order has already shipping or can be shipped\nif (!$order->canShip()) {\n    throw new MagentoFrameworkExceptionLocalizedException(\n        __('You cant create the Shipment.'));\n}\n\n\/\/ Initializzing Object for the order shipment\n$convertOrder = $this->_objectManager->create('MagentoSalesModelConvertOrder');\n$shipment = $convertOrder->toShipment($order);\n\n\/\/ Looping the Order Items\nforeach ($order->getAllItems() as $orderItem) {\n\n\/\/ Check if the order item has Quantity to ship or is virtual\n    if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) {\n        continue;\n    }\n    $qtyShipped = $orderItem->getQtyToShip();\n\n\/\/ Create Shipment Item with Quantity\n    $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped);\n\n\/\/ Add Shipment Item to Shipment\n    $shipment->addItem($shipmentItem);\n}\n\n\/\/ Register Shipment\n$shipment->register();\n$shipment->getOrder()->setIsInProcess(true);\ntry {\n\n\/\/ Save created Shipment and Order\n    $shipment->save();\n    $shipment->getOrder()->save();\n\n\/\/ Send Email\n    $this->_objectManager->create('MagentoShippingModelShipmentNotifier')\n        ->notify($shipment);\n} catch (Exception $e) {\n    throw new MagentoFrameworkExceptionLocalizedException(\n        __($e->getMessage())\n    );\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The method to&nbsp;create shipment programmatically in Magento 2&nbsp;is a convenient way to add more shipping methods to the Magento 2 store. Online shopping is gaining&#8230;<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[34],"tags":[],"class_list":["post-36","post","type-post","status-publish","format-standard","hentry","category-magento"],"acf":[],"_links":{"self":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/36","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/comments?post=36"}],"version-history":[{"count":4,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/36\/revisions"}],"predecessor-version":[{"id":17759,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/posts\/36\/revisions\/17759"}],"wp:attachment":[{"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/media?parent=36"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/categories?post=36"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/meetanshi.com\/blog\/wp-json\/wp\/v2\/tags?post=36"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}