How to Add a Custom Button to Admin Sales Order View in Magento2
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="Company_Module::pluginBefore" type="Company\Module\Plugin\PluginBefore" />
</type>
</config>
2. Then in Company/Module/Plugin/PluginBefore.php
<?php
namespace Company\Module\Plugin;
class PluginBefore
{
protected $view;
public function __construct(
\Magento\Sales\Block\Adminhtml\Order\View $view
)
{
$this->view = $view;
}
public function beforePushButtons(
\Magento\Backend\Block\Widget\Button\Toolbar\Interceptor $subject,
\Magento\Framework\View\Element\AbstractBlock $context,
\Magento\Backend\Block\Widget\Button\ButtonList $buttonList
) {
$orderIds = $this->view->getOrderId();
$message = 'Are you sure you want to do this?'.$orderIds;
$this->_request = $context->getRequest();
if($this->_request->getFullActionName() == 'sales_order_view'){
$buttonList->add(
'mybutton',
[
'label' => __('Update Log'),
'onclick' => "confirmSetLocation('{$message}','{}')",
'class' => 'go'
],
-1
);
}
}
}
3. Clear Magento cache and run update command
php bin/magento setup:upgrade
4. Finally you will see the button in backened