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!
<?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.