Hello Magento Friends 👋,

How are you doing? Today I am going to explain How to cancel order Programmatically without disabling URL security key in Magento 2?. In case you missed out on the previously published blog, it is right here. Magento 2: How To Display Count Of Product with Page Title on Category Page.

Whenever the admin tries to cancel an order from the controller using order ID, it is required to disable the URL security key from Magento 2 admin configuration. Thereby, I am here with the code to cancel order programmatically without disabling  URL security key in Magento 2. Let’s get started 🚀

Steps to cancel order Programmatically without disabling URL security key in Magento 2:

Step 1: Navigate to the following path:

app/code/VENDOR/EXTENSION/Controller/Adminhtml/Order/Cancel.php

And add the below code:

Step 2: Now, move to the following path:

app/code/VENDOR/EXTENSION/view/adminhtml/ui_component/sales_order_grid.xml

Now, add the below code:

Step 3: Next, navigate to the following path:

app/code/VENDOR/EXTENSION/UI/Component/Listing/Column/Actions.php

And add the below code:

Step 4: Lastly, navigate to the following path:

app/code/VENDOR/EXTENSION/etc/adminhtml/routes.xml

Finally, add the below code:

That’s it!

Conclusion:

Hence, hopefully, everyone is able to cancel order Programmatically without disabling URL security key in Magento 2. If you face any hardship while executing the code, kindly mention in the comment section below. Also, share the article with your Magento developer friends. Stay connected!

Happy Coding 😊

[Total: 3   Average: 5/5]