How To Add A Custom Button On The Admin Sales Order View In Magento 2

Hello Everyone,       

In this blog, we will learn how to Add a Custom Button to Admin Sales Order View in Magento 2.

Once an order is placed it’s the store owner’s responsibility to deliver a successful order.

Sales Order View is a very important section for Order Related Information to fulfill the customer order.          

Without wasting your time, let us guide you straight away. Follow the easy step given below to Add a Custom Button on Admin Sales Order View in Magento 2.

STEPS FOR ADDING CUSTOM BUTTON ON ADMIN SALES ORDER VIEW IN MAGENTO 2

Step 1: Create di.xml file

app/code/Vendor/Extension/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="CustomButtonOrderView" type="Vendor\Extension\Plugin\CustomBtnOrderView" />

    </type>

</config>

Step 2: Create CustomBtnOrderView.php file

app/code/Vendor/Extension/Plugin/CustomBtnOrderView.php

<?php

namespace Vendor\Extension\Plugin;

use Magento\Backend\Model\UrlInterface;

use Magento\Framework\ObjectManagerInterface;

class CustomBtnOrderView

{

    protected $object_manager;

    protected $_backendUrl;

    public function __construct(

        ObjectManagerInterface $om,

        UrlInterface $backendUrl

    ) {

        $this->object_manager = $om;

        $this->_backendUrl = $backendUrl;

    }

    public function beforeSetLayout(\Magento\Sales\Block\Adminhtml\Order\View $subject)

    {

        $sendOrder = $this->_backendUrl->getUrl('sales/send/order/order_id/'.$subject->getOrderId() );

        $subject->addButton(

            'sendordersms',

            [

                'label' => __('Send SMS'),

                'onclick' => "setLocation('" . $sendOrder. "')",

                'class' => 'ship primary'

            ]

        );

        return null;

    }

}

Step 3: Finally run the below commands

$ php bin/magento cache: clean

$ php bin/magento cache: flush

Step 4: Output:

Final Thoughts:

So this was the easiest way which we have told you in this blog. This is how you can Add a Custom Button in Admin Order View in Magento 2. Hope you liked the blog.

So quickly go to the comment box and tell me how you like this blog.

Stay tuned with us on our site to get new updates on Magento.

Thanks for reading and visiting our site.

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

Your email address will not be published. Required fields are marked *