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!

One thought on “How to add custom button in admin order detail page in magento 2

  1. Hello Sohel,

    I just try to figure out, how to add a button to sales order invoice page. I found a couple of solutions, that I not really understand. Your solution seems to be the easiest way. Can you help me please???

    I have created some documents like order confirmation, proforma invoice…
    Now I want to add buttons to the invoice edit page to print the documents. I need the invoice_id and the store_id in my link to the document-php file. I dont really understand, how and where to add the buttons.

    It would be great, if you could give me some help.

    Best regards,

    Christian

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.