How to Create Shipment Programmatically in Magento 2
The method to create shipment programmatically in Magento 2 is a convenient way to add more shipping methods to the Magento 2 store.
Online shopping is gaining popularity and merchants require to fulfill the order with the best facility in order to stand the competition. Shipping is one of them.
Shipping is an important factor in conversion, and these findings from Statista prove the same:
- Shipping terms are an important factor when ordering online – low acceptance rate for high shipping costs
- Even with free shipping, 80% of shoppers expect delivery within seven days at the latest
- Shipment tracking and delivery notifications are the most-used services offered by shipping providers and for that you need to add tracking number to current order shipment in Magento 2.
- Low use of fast shipping and scheduled delivery options; young shoppers use them the most
- Younger online shoppers are more likely to use express shipping options to avoid long delivery times
- Online shoppers are open to new delivery methods (e.g. via robots and drones)
Creating shipment in Magento 2 can be complex, but not with the programmatic method, which will save your time!
Use the following code to create shipment programmatically for order in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<?php // Loading the Order $order = $this->_objectManager->create('MagentoSalesModelOrder') ->loadByAttribute('increment_id', '000000001'); //OR $order = $this->_objectManager->create('MagentoSalesModelOrder') ->load('1'); // Check if order has already shipping or can be shipped if (!$order->canShip()) { throw new MagentoFrameworkExceptionLocalizedException( __('You cant create the Shipment.')); } // Initializzing Object for the order shipment $convertOrder = $this->_objectManager->create('MagentoSalesModelConvertOrder'); $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()) { continue; } $qtyShipped = $orderItem->getQtyToShip(); // Create Shipment Item with Quantity $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped); // Add Shipment Item to Shipment $shipment->addItem($shipmentItem); } // Register Shipment $shipment->register(); $shipment->getOrder()->setIsInProcess(true); try { // Save created Shipment and Order $shipment->save(); $shipment->getOrder()->save(); // Send Email $this->_objectManager->create('MagentoShippingModelShipmentNotifier') ->notify($shipment); } catch (Exception $e) { throw new MagentoFrameworkExceptionLocalizedException( __($e->getMessage()) ); } |
Let me know if you have any questions or faced any issue while following this method for creating Shipment Programmatically in Magento 2 in the Comments section below. I’d be happy to help.
Related posts:
- How to Create Quote & Order Programmatically in Magento 2
- How to Generate PDF Programmatically in Magento 2
Do share the solution with fellow Magento developers via social media.
Thank you.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
5 Comments
hi,
Thanks for the article. I am getting an error on email sending.
Error: Call to a member function getParentItem() on null in /Applications/MAMP/htdocs/myproject/vendor/magento/module-sales/view/frontend/templates/email/shipment/items.phtml:23
I tried using Magento\Sales\Model\Order\Email\Sender\ShipmentSender; also and same error i am getting.
Hello Mujahidh,
You can use the ‘Magento\Shipping\Model\ShipmentNotifier’ to notify the customer about shipment.
The same is working properly from our end.
Thank You
Thank you Meetanshi.. Was really helpgul
What do you need to call to create the shipping label at the same time so that it would call the UPS api for example.
You will need to pass required shipping parameters to UPS API to generate Shipping Labels.