How to add custom button in admin order detail page in magento 2

Suppose vendor name ‘SR‘ and Module name ‘RewriteSales

Create a plugin in SR/RewriteSales/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\Context">
        <plugin name="add_custom_button_sales_veiw" type="SR\RewriteSales\Plugin\Widget\Context" sortOrder="1"/>
    </type>
</config>

Then create in SR/RewriteSales/Plugin/Widget/Context.php

namespace SR\RewriteSales\Plugin\Widget;


class Context
{
    public function afterGetButtonList(
        \Magento\Backend\Block\Widget\Context $subject,
        $buttonList
    )
    {
        if($subject->getRequest()->getFullActionName() == 'sales_order_view'){
            $buttonList->add(
                'custom_button',
                [
                    'label' => __('Custom Button'),
                    'onclick' => "setLocation('window.location.href')",
                    'class' => 'ship'
                ]
            );
        }

        return $buttonList;
    }
}

Enjoy!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s