I need to cancel order using tracking number in magento2?

  • I want to do it programatically.. Aug 10, 2021 at 9:06

2 Answers 2


simple code to cancel the order

   protected $orderRepository;

    public function __construct(
        \Magento\Sales\Api\OrderRepositoryInterfac $orderRepository,
        $this->orderRepository = $orderRepository;

    public function cancel($id)
        $order = $this->orderRepository->get($id);
        if ($order->canCancel()) {

check the below link to get order tracking info. Get tracking info from order/shipment object

I am not sure using a tracking id to cancel the order is possible or not but you have a tracking id then you can filter orders by tracking id and get order id by order id. you can cancel the order using above code.

  • I am getting the tracking no from the above link.. Aug 10, 2021 at 11:04
  • I am getting the tracking number in the Cancel.php file located in app\code\Magento\Sales\Controller\Adminhtml\Order ..in the public function execute() function Aug 10, 2021 at 11:06
  • I need to call the API using tracking number..Can I call the API in the public function execute() of Cancel.php file of app\code\Magento\Sales\Controller\Adminhtml\Order Aug 10, 2021 at 11:07
  • How to call getConfigData('filedname') method in order cancel module Aug 11, 2021 at 14:58


There's no direct way to get the data using the Magento API. An order can have multiple shipments with different tracking numbers.
I suggest you check https://magento.redoc.ly/2.3.7-admin/tag/shipments
If you use the search criteria (see this question as an example), you can specify the tracking number to filter the results.
From there you can get the order_id, which you can then use to cancel the order (see below; answer from @Devidas should also work).

Original (updated) answer - leaving it here in case it is still useful.

Tracking numbers are saved in the sales_shipment_track table. See \Magento\Sales\Api\ShipmentTrackRepositoryInterface::getList with which you can use search criteria to filter tracks with a specific tracking number.

You can take a look at this answer how you can apply a search criteria. Basically, if you inject \Magento\Framework\Api\SearchCriteriaBuilder in the contructor, you can then try:

$trackNo = 'TEST_TRACK_NO';
$searchCriteria = $this->searchCriteriaBuilder->addFilter('track_number', $trackNo);

And then get the data with:

$tracks = $this->shipmentTrackRepository->getList($searchCriteria->create())

Where shipmentTrackRepository is the injected ShipmentTrackRepositoryInterface interface in the constructor.

It returns the data from that table which has the order id as well.

@return \Magento\Sales\Api\Data\ShipmentTrackInterface Shipment track interface.

You can then get the order_id using

$orderId = $tracks->getData()[0]['order_id']

which you can then supply as an argument in \Magento\Sales\Api\OrderManagementInterface::cancel to cancel the order.

  • See also if \Magento\Sales\Api\ShipmentTrackRepositoryInterface::getList helps. I think track id refers to entity_id on that table rather than tracking number, and maybe its a good idea to get the entire list and try to filter it. I'll take a look later and see if I can provide an example.
    – Erlis D.
    Aug 10, 2021 at 12:04
  • Ok..Thank You..I will try Aug 10, 2021 at 12:11
  • But Actually I have to call third party API to cancel the order..I have created the order using the third party API..Now to cancel the order I need tracking number..I want to know where to call API to cancel the order from backend in magento 2? Aug 10, 2021 at 12:13
  • I'm a bit confused... You want to call the Magento API to cancel the order externally? Or are you trying to cancel the order on the 3rd party and want to send the tracking number to the 3rd party API? Or are you just trying to cancel an order in Magento using a tracking number stored in Magento?
    – Erlis D.
    Aug 10, 2021 at 13:02
  • I am just trying to cancel an order in Magento using a tracking number stored in Magento using CURL for API Aug 10, 2021 at 13:27

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.