Magento2 – Remove Cancel Button on Admin Order Info Page

magento2orders

I need to disable the cancellation of orders that "meet" a certain criteria.
Basically ,i need to check if the order id exists in a custom table, and if it does, cancellation from the order form is wont be allowed but it can still be canceled programmatically.

Can any of you tell me how this could be done?

Best Answer

Ok I found a solution for this one. Solution is based on this tutorial

Rahaha\RahahaWebService\etc\di.xml

<type name="Magento\Backend\Block\Widget\Button\Toolbar">
    <plugin name="orderFormToolbarButtons" type="Rahaha\RahahaWebService\Plugin\Block\Widget\Button\Toolbar" />
</type>

Rahaha\RahahaWebService\Plugin\Block\Widget\Button\Toolbar.php

<?php
namespace Rahaha\RahahaWebService\Plugin\Block\Widget\Button;

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

class Toolbar
{
    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];
        }

        $order = $context->getOrder();

        if (!empty($order['rahaha_transaction_id'])) {
            $buttonList->remove('order_edit');
            $buttonList->remove('order_invoice');
            $buttonList->remove('order_hold');
            $buttonList->remove('order_ship');
            $buttonList->remove('order_cancel');
            $buttonList->remove('order_reorder');
        }


        return [$context, $buttonList];
    }
}
Related Topic