How to Get Tracking Information From Shipment in Magento 2
A customer-centric mindset leads to success in business. More now than ever, owing to the competition in E-commerce, enhancing the customers’ shopping experience is a never-ending process.
And my job is to help the readers who are Magento 2 store owners, in implementing features that can improve the customer experience of their store.
And as a part of it, today, I am posting a programmatic solution to get tracking information from shipment in Magento 2. To get it done you need to add tracking number to current order shipment in Magento 2.
You can use this solution in your Magento 2 store when you notify your customers about the order placement. For example, you are sending an email or SMS to notify the customers about their recent order placed and you need to add the carrier name and the tracking number of the item to be delivered. The below method will help you get the tracking information from the order/shipment.
Doing so will be easier for customers to track the order in Magento 2. Everyone is excited about receiving the items ordered online, it is a general human tendency. Acting upon this human emotion to level up your customer experience game is a smart thing to do!
For this solution, I’ll be using the shipment details entered from the backend as shown below:
Obtain these details from the shipment and then use it along with the Email or SMS that you send for notifying the customers about successful order placement.
Method to get tracking information from shipment in Magento 2:
Add below code in your events.xml file at Vendor\Extension\etc
1 2 3 4 5 6 |
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> <event name="sales_order_shipment_save_after"> <observer name="track_shipment" instance="Vendor\Extension\Observer\Shipment" /> </event> </config> |
Create new Shipment.php file at Vendor\Extension\Observer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php namespace Vendor\Extension\Observer; use Magento\Framework\Event\ObserverInterface; class Shipment implements ObserverInterface { public function execute(\Magento\Framework\Event\Observer $observer) { try { $shipment = $observer->getEvent()->getShipment(); $tracksCollection = $shipment->getTracksCollection(); foreach ($tracksCollection->getItems() as $track) { $trackNumber = $track->getTrackNumber(); $carrierName = $track->getTitle(); } } catch (\Exception $e) { } } } |
That’s it.
Any doubts? Please mention them in the Comments section below. I’d be happy to help you out.
Also, do not forget to share the post with fellow Magento 2 store owners via social media and help them in enhancing the customer experience of the store! Contributing to making E-commerce a better place to shop is a nice thing to do after all!
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.
Prev
How To Create Bundle Products in Magento 2
How to Process Message Queue in Magento 2
Next