How to cancel an order programmatically in Magento 2?

Are there ways to programmatically cancel Magento orders in Magento?

By default, Magento orders cannot be canceled programmatically on the front end. So if you want to cancel an order for any reason, you must do so either by writing code to do so, which is technically difficult and requires coding skills, or by using a third-party extension such as Cancel Orders extension, which allows you to cancel Magento orders without doing it programmatically. With the extension, customers can cancel orders from the front end on their own, and store administrators can automatically cancel pending purchases after a certain period.

How to configure the extension to cancel Magento 2 orders without doing it programmatically?

To cancel Magento 2 orders without doing it programmatically follow the steps: 

Step 1. Navigate to Stores > Configuration > Amasty Extensions > the Cancel Orders tab to configure the basic settings.

Step 2. Set Yes in the Enabled dropdown to make the extension work.

Step 3. In the Allow for Selected Group(s) section, you need to define customer groups for which order cancellation will be on.

Step 4. Choose orders that can be canceled in the Allowed Order Status(es) section.

Step 5. Select the desired option from the Refund Paid Processing Orders Automatically dropdown:

  • Disabled - Processing Order can be canceled if it has not yet been invoiced.
  • Online - order will be refunded online.
  • Offline - orders will be refunded offline.

All items in canceled orders will be returned to stock automatically.

The extension processes requests as follows:

  • Pending requests are auto-converted to Cancelled.
  • Processing orders without invoices and Suspected fraud are converted to Cancelled.
  • For Processed orders with invoices, money will be refunded: if the auto refund is selected, a credit memo is created and the status of the order changes to Closed. And then, the status changes to Cancelled, and a comment is added.
How can we help you?

Didn’t you find the answer to your question? We are always happy to help you out.