Is there anyone has experience to get order or shipment details by tracking number?

I saw few rest apis available in magento 2.


POST   /V1/returns/:id/tracking-numbers
GET    /V1/returns/:id/tracking-numbers


1 Answer 1


Inject \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory into your class constructor and filter by track_number:

use Magento\Sales\Api\Data\ShipmentTrackInterface;
use Magento\Sales\Model\Order\Shipment;
use Magento\Sales\Model\ResourceModel\Order\Shipment\Track\CollectionFactory as TrackCollectionFactory;

class Example

    /** @var  \Magento\Sales\Model\ResourceModel\Order\Shipment\Track\Collection */
    protected $trackingCollection;

    public function __construct(TrackCollectionFactory $collectionFactory)
        $this->trackingCollection = $collectionFactory->create();

    public function getShipmentDetailsFromTrackingNumber($number)
        try {
                ->addFieldToFilter(ShipmentTrackInterface::TRACK_NUMBER, $number);
            /** @var Shipment\Track $tracking */
            $tracking = $this->trackingCollection->getFirstItem();
            /** @var Shipment $shipment */
            $shipment = $tracking->getShipment();

            return $shipment->getData();
        catch (\Magento\Framework\Exception\LocalizedException $e) {}

        return null;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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