It’s very common that we need a feature to cancel an order Programatically in Magento 2. Instead of factory method, we use Magento/Sales/Api/OrderManagementInterface as it’s advisable to use service contracts in Magento 2.
Below snippet will help you cancel an order programatically in Magento 2.
protected $orderManagement; public function __construct( ... \Magento\Sales\Api\OrderManagementInterface $orderManagement, .... ) { .... $this->orderManagement = $orderManagement; .... } public function cancelOrder() { $this->orderManagement->cancel($orderId); }
Leave a Reply