I need to cancel order using tracking number in magento2?
2 Answers
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()) {
$order->cancel();
$this->orderRepository->save($order);
}
}
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 number in the Cancel.php file located in app\code\Magento\Sales\Controller\Adminhtml\Order ..in the public function execute() function Commented 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 Commented Aug 10, 2021 at 11:07
-
How to call getConfigData('filedname') method in order cancel module Commented Aug 11, 2021 at 14:58
UPDATE:
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.Commented Aug 10, 2021 at 12:04 -
-
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? Commented 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.Commented 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 Commented Aug 10, 2021 at 13:27