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); }
Did this post help you?
Tutsplanet brings in-depth and easy tutorials to understand even for beginners. This takes a considerable amount of work. If this post helps you, please consider supporting us as a token of appreciation:
- Just want to thank us? Buy us a Coffee
- May be another day? Shop on Amazon using our links.
Your prices won't change but we get a small commission.
Leave a Reply