How to add cancel order functionality on myaccount page of customers so lease do the needful

enter image description here

please do the needful

  • No i want programmatically.
    – sangan
    Apr 11, 2017 at 10:11

2 Answers 2


Create a controller file


namespace Vendor\Module\Controller\Cancelorder;

use Magento\Framework\Controller\ResultFactory;

class Index extends \Magento\Framework\App\Action\Action
    protected $resultPageFactory;
    protected $_order;

    public function __construct(
        \Magento\Framework\App\Action\Context $context,
        \Magento\Framework\View\Result\PageFactory $resultPageFactory,
        \Magento\Sales\Model\Order $order,
        array $data = [])
        $this->resultPageFactory = $resultPageFactory;
        $this->_order = $order;
        return parent::__construct($context,$data);

    public function execute()
        $resultRedirect = $this->resultFactory->create(ResultFactory::TYPE_REDIRECT);
        $orderId = $this->getRequest()->getParam('orderid');
        $order = $this->_order->load($orderId);
            $this->messageManager->addSuccess(__('Order has been canceled successfully.'));
        } else {
            $this->messageManager->addError(__('Order cannot be canceled.'));
        return $resultRedirect;

Now override vendor/magento/module-sales/view/frontend/templates/order/recent.phtml file at app/design/frontend/your_vendor/your_theme/Magento_Sales/templates/order/recent.phtml and add below code in this file.

<?php if($_order->canCancel()) : ?>
    <a href="<?php echo $this->getUrl('module/cancelorder/index').'?orderid='.$_order->getEntityId(); ?>">Cancel Order</a>
<?php endif; ?>
  • For Order cancel email; which event can be used. This is not working order_cancel_after Oct 11, 2018 at 15:49
  • @Slimshadddyyy Magento doesn't send any cancellation email. You have to create your custom email function for this. Oct 12, 2018 at 5:28
  • Magento sends an email on order status update. Are you talking about that? Oct 12, 2018 at 5:28
  • I need to send email when an order is cancelled from customer's side. I have developed custom module where customer can cancel order, but could not find event which sends email. order_cancel_after is not working although when an order is cancelled Oct 12, 2018 at 14:22
  • You have to create your custom functionality for sending the email. Try to use plugin on cacel order function. Oct 13, 2018 at 4:37

We have created module here: https://github.com/magepow/magento2-cancel-order, you can use it

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.