How to Add Tracking Number to the Current Order Shipment in Magento 2
E-commerce giants like Amazon, Flipkart, and eBay allow customers to track their orders. Whenever the shipment gets delayed, the customers and the merchants can check where the product has reached by leveraging tracking numbers and the merchants can solve the problem quickly. Shipment tracking is an important aspect of supply chain management.
For first-time shoppers at an online store may be particularly worried if there is a delay in order delivery for some reason. To gain trust and loyalty from customer, a tracking number is paramount that shows where the product has reached and how long it will take to deliver.
Here are the advantages of leveraging tracking numbers for your E-commerce store:
- Deliver better shopping experience:
- Reduces the chances of delays and missing products:
- Increases performance and process efficiency
Hence, it is beneficial not only to customers but also for the merchants to add tracking numbers to the current order shipment in your Magento 2.
For Magento 2 store owners, you can implement the below method to add tracking number to the current order shipment in Magento 2.
With this solution, enhance the shopping experience of the store and win customers’ trust!
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 52 53 54 55 56 57 58 59 60 61 62 63 |
<?php // Load the order $order = $this->_objectManager->create('Magento\Sales\Model\Order') ->loadByAttribute('increment_id', '000000001'); //OR $order = $this->_objectManager->create('Magento\Sales\Model\Order') ->load('1'); // Check if order has already shipped or can be 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 is virtual or has quantity to ship if (!$orderItem->getQtyToShip() || $orderItem->getIsVirtual()) { continue; } $qtyShipped = $orderItem->getQtyToShip(); // Create shipment item with qty $shipmentItem = $convertOrder->itemToShipmentItem($orderItem)->setQty($qtyShipped); // Add shipment item to shipment $shipment->addItem($shipmentItem); } // Register shipment $shipment->register(); $data = array( 'carrier_code' => 'ups', 'title' => 'United Parcel Service', 'number' => 'TORD23254WERZXd3', // Replace with your tracking number ); $shipment->getOrder()->setIsInProcess(true); try { // Save created shipment and order $track = $this->_objectManager->create('Magento\Sales\Model\Order\Shipment\TrackFactory')->create()->addData($data); $shipment->addTrack($track)->save(); $shipment->save(); $shipment->getOrder()->save(); // Send email $this->_objectManager->create('Magento\Shipping\Model\ShipmentNotifier') ->notify($shipment); $shipment->save(); } catch (\Exception $e) { throw new \Magento\Framework\Exception\LocalizedException( __($e->getMessage()) ); } |
That’s it.
Any doubts about the solution can be mentioned in the Comments section below. I’d be happy to help.
Also, please share the post with fellow Magento 2 developers via social media.
Thank you.
Related articles:
- How to Create Custom Carrier Trackers in Magento 2
- How to Configure Magento 2 Custom Order Number Extension
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.
6 Comments
Hello,
I’ve stumbled with your article and tried to aply it to my magento 2.3.5 store, but have no clue when that code is placed, can you get me some help
Hello,
You can use this code in any class where you want to generate the shipment dynamically.
Thanks.
Great articles!.
Thanks for your helps.
Hey! Thank you for these nice words.
Happy to help 🙂
Hello,
This is great article ! we can easily update shipping tracking number. Thank you very much !!
I would like to know, if there is any option to add ‘Ship Date’ value at Shipments tab in admin sales order details page.
It requires custom code and purely depends on where and how you want to show it.