Get tracking Id magento 2
-
05-02-2021 - |
Question
I want to Get Tracking id when shipping the order, For it, I use "sales_order_shipment_save_after" Observer. And I try but I got the blank array. My code is
$shipment = $observer->getEvent()->getShipment();
$order = $shipment->getOrder();
$shipmentCollection = $order->getShipmentsCollection();
$tracksCollection = $order->getTracksCollection();
foreach ($tracksCollection->getItems() as $track) {
$trackNumbers[] = $track->getTrackNumber();
}
I also try other solution which is given in the old post but I am not got any result.
I am also try $observer->getEvent()->getTrack();
but also got blank in data.
Solution
Try with below code.
//Get shipment details.
$shipment = $observer->getEvent()->getShipment();
//Get shipment tracking info.
$trackingNumbers = array();
foreach ($shipment->getAllTracks() as $track) {
$trackingNumbers[] = $track->getNumber();
};
echo '<pre>', print_r($trackingNumbers);exit;
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange