0

I am getting the order information using

 $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
 $order = $objectManager->create('Magento\Sales\Api\OrderRepositoryInterface')->get($orderid);

Then I am iterating the $order to get the ordered item related info. How can I get the tracking number for those items as well?

 <?php foreach ($order->getAllVisibleItems() as $item): ?>
    // how to use $item to get tracking number
 <?php?>

I came to this this solution but this didn't work and I don't want to iterate it $order->getTracksCollection() as I am already in a loop of <?php foreach ($order->getAllVisibleItems() as $item): ?>

 $tracksCollection = $order->getTracksCollection();
 foreach ($tracksCollection->getItems() as $item) {
    echo $track->getTrackNumber(); // Tracking number
 }
1

Try this,

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->load($orderid);
$tracksCollection = $order->getTracksCollection();
foreach ($tracksCollection->getItems() as $track) {
    $trackNumbers[] = $track->getTrackNumber();
}

The problem seems you're not loading order object.

Hope this helps.

  • thanks it worked – summu May 23 at 13:16
  • this will give tracking number for all items of an order irrespective of it is configurable or simple type. – summu May 23 at 13:48
  • Can you explain it how it is connected I table level(database), I see sales_order_item has all items of an order, order_id of sales_order_item = entity_id of sales_order, but how it is connected to the tracking table i.e, sales_shipment_track – summu May 23 at 13:50
  • You can go to the file called order.php that reside getTracksCollection() and that will fetch tracking information from this class Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory – Prathap Gunasekaran May 23 at 13:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.