0

I need to remove Order cancel button from order view page in admin side, as well from mass action in magento 2.3.2 for particular user.

1

Create file in adminhtml app\code\Ketan\Buttons\etc\adminhtml\di.xml

<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Backend\Block\Widget\Button\Toolbar">
        <plugin name="RemoveCancelButton" type="Ketan\Buttons\Plugin\Widget\Context" />
    </type>
</config>

Create Plugin file app\code\Ketan\Buttons\Plugin\Widget\Context.php

<?php

namespace Ketan\Buttons\Plugin\Widget;

use Magento\Backend\Block\Widget\Button\ButtonList;
use Magento\Backend\Block\Widget\Button\Toolbar as ToolbarContext;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\AbstractBlock;

class Context
{
    protected $coreRegistry;
    protected $authSession;
    public function __construct(
        Registry $coreRegistry,
        \Magento\Backend\Model\Auth\Session $authSession,
    ) {
        $this->coreRegistry = $coreRegistry;
        $this->authSession = $authSession;
    }

    /**
     * @param ToolbarContext $toolbar
     * @param AbstractBlock $context
     * @param ButtonList $buttonList
     * @return array
     */
    public function beforePushButtons(
        ToolbarContext $toolbar,
        \Magento\Framework\View\Element\AbstractBlock $context,
        \Magento\Backend\Block\Widget\Button\ButtonList $buttonList
    ) {
        if (!$context instanceof \Magento\Sales\Block\Adminhtml\Order\View) {
            return [$context, $buttonList];
        }
        $user = $this->authSession->getUser();
        $userName = $user->getUsername();
        if($userName=='admin' || $userName=='superadmin'){
            $buttonList->remove('order_cancel');
        }

        return [$context, $buttonList];
    }

}
0

Create plugin to remove cancel button from order view page.

namespace NameSpace\OrderManagement\Plugin\Sales\Block\Adminhtml\Order;

use NameSpace\OrderManagement\Helper\Data;
use Magento\Sales\Block\Adminhtml\Order\View;
use Magento\Framework\View\LayoutInterface;

class ViewPlugin {

/**
 * @var Data
 */
protected $helper;

public function __construct(Data $helper)
{
    $this->helper = $helper;
}//end __construct()

public function beforeSetLayout(View $view, LayoutInterface $layout)
{
    if ($this->helper->isAllowedtoDisplayCancelButton()) {
        $view->removeButton('order_cancel');
    }
}//end beforeSetLayout()

}//end class

create di.xml

<type name="Magento\Sales\Block\Adminhtml\Order\View">
    <plugin name="removeBackButton" type="NameSpace\OrderManagement\Plugin\Sales\Block\Adminhtml\Order\ViewPlugin"/>
</type>

create Helper file and add below function

public function isAllowedtoDisplayCancelButton()
{
    return !$this->authorization->isAllowed('Magento_Backend::all') &&
    $this->authorization->isAllowed('Magento_Sales::cancel');
}//end isCustomerSupportAgent()

and add Magento_Sales::cancel resource in your acl.xml

Hope it will work for you please accept my answer.

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.