Magento order view toolbar has a handful of options to quickly perform tasks such as editing orders, reordering the same order, invoicing etc but these are not always needed.

We had a requirement from one of our clients where they wanted to remove the Edit order button and make the invoice button primary.

To achieve this, most people would simply hide it via the CSS but is that the most efficient way of doing it? Certainly not!

If not CSS display: none; then what?

We all know that Magento is a complex system and how important it is to do things in the best way possible. So, for us to achieve this, we found a public method pushButtons()in Magento\Backend\Block\Widget\Button\Toolbar class which injects the buttons on order view page.

Since pushButtons() is a public method so we can simply create a Magento Before Plugin to remove Edit button from the toolbar.

Development

First thing first, let’s create a skeleton module and register in Magento.

You should:

  1. Create new etc/module.xml configuration file with module name MagePress_Sales and setup version.
  2. Create registration.php file in order to include module into composer autoload workflow.
  3. Run bin/magento setup:upgrade command

Next, let’s create a di.xml file in ./etc/ directory and declare our plugin:

<?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="remove_edit_view_order_toolbar" type="MagePress\Sales\Plugin\Magento\Backend\Block\Widget\Button\Toolbar"/>
    </type>
</config>

Create a new class called Toolbar in Plugin\Magento\Backend\Block\Widget\Button

<?php
namespace MagePress\Sales\Plugin\Magento\Backend\Block\Widget\Button;

class Toolbar
{
}

Finally, since we want to modify the buttons list before the page is loaded so we’ll create a before plugin to manipulate the block. Your final Toolbar.php file should look like this:

<?php
namespace MagePress\Sales\Plugin\Magento\Backend\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
{
    /**
     * @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];
        }
        $buttonList->update('order_reorder', 'class', 'reorder primary');
        $buttonList->remove('order_edit');

        return [$context, $buttonList];
    }
}

… and that’s it! Now, run php bin/magento setup:upgrade command and the button should disappear from the order view page

Removed the Edit button using Magento Before Plugin

Any thoughts? Leave a comment and let us know if you have any questions.