Skip to content

sales_order_item_cancel Event Not Triggered when Canceling Order Programmatically using $this->orderManagement->cancel($orderId);

I’m currently working on a module that involves the programmatic cancellation of orders. To cancel orders, I am using $this->orderManagement->cancel($orderId); in my code. This appears to be working correctly as the order status and state change to ‘canceled’ and the products within the order also seem to be getting canceled appropriately.

However, I have noticed a discrepancy when it comes to the triggering of the sales_order_item_cancel event. When I manually cancel an order by clicking on the ‘cancel’ button in the order view, this event is invoked, which is what I expect.

The problem arises when an order is canceled programmatically using my module, as this event does not seem to get triggered. This is causing issues since this event is required by other modules to carry out their operations when an order is canceled.

Looking into the source code, I can see that the $this->orderManagement->cancel($orderId); call should trigger the cancel method in MagentoSalesModelOrderItem, which includes the dispatch of the sales_order_item_cancel event $this->_eventManager->dispatch('sales_order_item_cancel', ['item' => $this]);.

This leaves me puzzled as to why this event isn’t triggered the same way when orders are canceled programmatically as compared to when they are manually canceled from the admin panel. All the more confusing is that the rest of the cancellation process seems to work as expected, including the restocking of items.

Has anyone else encountered this issue or have any insight on why this might be happening? Any help would be greatly appreciated!

Thank you.