How to Add a Custom Button to Admin Sales Order View in Magento2
The cleanest solution I've seen so far is to use a plugin targeting 'beforeSetLayout'
This can target the exact block, saving the check for the current request, and also avoids the plugin being on 'getOrderId' which in my case could not be used as I needed to call getOrderId in my plugin method.
So this in di.xml
<type name="Magento\Sales\Block\Adminhtml\Order\View">
<plugin name="addMyButton" type="My\Module\Plugin\Block\Adminhtml\Order\View"/>
</type>
And then this in the file My\Module\Plugin\Block\Adminhtml\Order\View.php
public function beforeSetLayout(\Magento\Sales\Block\Adminhtml\Order\View $view)
{
$message ='Are you sure you want to do this?';
$url = '/mymodule/controller/action/id/' . $view->getOrderId();
$view->addButton(
'order_myaction',
[
'label' => __('My Action'),
'class' => 'myclass',
'onclick' => "confirmSetLocation('{$message}', '{$url}')"
]
);
}
After trying many different ways, this is the only solution I could find that seem to work without affecting other modules. I would love to see other solutions.
Option 1
Create a plugin in Company/Module/etc/adminhtml/di.xml
<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="MagePal_TestBed::pluginBefore" type="MagePal\TestBed\Plugin\PluginBefore" />
</type>
</config>
Then in Plugin/PluginBefore.php
namespace MagePal\TestBed\Plugin;
class PluginBefore
{
public function beforePushButtons(
\Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
\Magento\Framework\View\Element\AbstractBlock $context,
\Magento\Backend\Block\Widget\Button\ButtonList $buttonList
) {
$this->_request = $context->getRequest();
if($this->_request->getFullActionName() == 'sales_order_view'){
$buttonList->add(
'mybutton',
['label' => __('My Button'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
-1
);
}
}
}
Option 2
Create a plugin in Company/Module/etc/adminhtml/di.xml
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="\Magento\Sales\Block\Adminhtml\Order\View">
<plugin name="MagePal_TestBed::pluginBeforeView" type="MagePal\TestBed\Plugin\PluginBeforeView" />
</type>
</config>
Then in Plugin/PluginBeforeView.php
namespace MagePal\TestBed\Plugin;
class PluginBeforeView
{
public function beforeGetOrderId(\Magento\Sales\Block\Adminhtml\Order\View $subject){
$subject->addButton(
'mybutton',
['label' => __('My Buttion'), 'onclick' => 'setLocation(window.location.href)', 'class' => 'reset'],
-1
);
return null;
}
}
See Full Source code
Create DI file: app/code/YourVendor/YourModule/etc/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">
<virtualType name="SalesOrderViewWidgetContext" type="\Magento\Backend\Block\Widget\Context">
<arguments>
<argument name="buttonList" xsi:type="object">YourVendor\YourModule\Block\Adminhtml\Order\View\ButtonList
</argument>
</arguments>
</virtualType>
<type name="Magento\Sales\Block\Adminhtml\Order\View">
<arguments>
<argument name="context" xsi:type="object">SalesOrderViewWidgetContext</argument>
</arguments>
</type>
</config>
What we do here is:
- Set custom
context
argument into theOrder\View
block. This context is defined as a virtual type. - Define virtual type for a
widget context. We set custom
buttonList
argument with our own button list class.
Implement your button list class:
<?php
namespace YourVendor\YourModule\Block\Adminhtml\Order\View;
class ButtonList extends \Magento\Backend\Block\Widget\Button\ButtonList
{
public function __construct(\Magento\Backend\Block\Widget\Button\ItemFactory $itemFactory)
{
parent::__construct($itemFactory);
$this->add('mybutton', [
'label' => __('My button label')
]);
}
}