2

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

I saw few rest apis available in magento 2.

http://devdocs.magento.com/guides/v2.1/rest/list.html

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

Thanks

3

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 {
            $this->trackingCollection
                ->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;
    }
}
| improve this answer | |

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.