SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH SPECIAL OFFER: ENJOY 3 MONTHS OF SHOPIFY FOR $1/MONTH
customer cancel order
Magepow.com

Customer Cancel Order Magento 2

Hi everyone, Today I will show you how to make function customer cancel the order in Magento 2

Step 1. Create Index.php block file

app/code/Magepow/CancelOrder/Controller/Cancelorder/Index.php

<?php

namespace Magepow\CancelOrder\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);
        if($order->canCancel()){
            $order->cancel();
            $order->save();
            $this->messageManager->addSuccess(__('Order has been canceled successfully.'));
        } else {
            $this->messageManager->addError(__('Order cannot be canceled.'));
        }
        $resultRedirect->setUrl($this->_redirect->getRefererUrl());
        return $resultRedirect;
    }
}

Step 2: Create sales_order_history.xml and customer_account_index.xml file to show the cancel button on my account page

/app/code/Magepow/CancelOrder/view/frontend/layout/sales_order_history.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="sales.order.history" template="Magepow_CancelOrder::order/history.phtml"/>
    </body>
</page>

/app/code/Magepow/CancelOrder/view/frontend/layout/customer_account_index.xml

<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
    <body>
        <referenceBlock name="customer_account_dashboard_top" template="Magepow_CancelOrder::order/recent.phtml"/>
    </body>
</page>

Step 3. Create routes.xml file

/app/code/Magepow/CancelOrder/etc/frontend/routes.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
    <router id="standard">
        <route id="cancelorder" frontName="cancelorder">
            <module name="Magepow_CancelOrder" />
        </route>
    </router>
</config>

Step 4. Create history.phtml and recent.phtml template file to show the cancel button in my account page

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

<?php if($_order->canCancel()): ?>
    <a href="<?php echo $this->getUrl('cancelorder/cancelorder/index').'?orderid='.$_order->getEntityId(); ?>" class="action cancel"><?= /* @escapeNotVerified */ __('Cancel Order') ?></a>
<?php endif; ?>

Override vendor/magento/module-sales/view/frontend/templates/order/history.phtml file at app/code/Magepow/CancelOrder/templates/order/history.phtml and add below code in this file.

<?php if($_order->canCancel()): ?>
    <a href="<?php echo $this->getUrl('cancelorder/cancelorder/index').'?orderid='.$_order->getEntityId(); ?>" class="action cancel"><?= /* @escapeNotVerified */ __('Cancel Order') ?></a>
<?php endif; ?>

This is result

This is Result

Done. Hope this article will helpful for you.