How To

How to add custom Button on Admin Sales Order View page in Magento 2

For small Businesses, Order is like a heartbeat that makes it alive. Once an order is placed the real war for the store owner is begin to ensure successful order delivery by making the customer happy. CMS like Magento plays a vital role to collect and manage all that’s required customer information in one place. The Magento Sales order view is the most critical area for the store owner to work with where they can see complete order details like name, contact details, shipping and billing address, payment methods they have used etc.
As business varies, several times you think that what if there is such option, therefore, I can save my time to complete things before time and that’s how Magento Custom Development comes into the picture. Till date, we have received so many requests for customizing sales order view section to make stuff easier for the store owner. So, we decided to share one small trick that will help you to add custom Button on the Admin Sales Order View page in Magento 2.
Simply follow this to steps and you are done!
Firstly, we have to create “di.xml” file inside our custom extension folder using below code.
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="Vendor_Extension::sendOrderView" type="Vendor\Extension\Plugin\PluginBtnOrderView" />
    </type>
</config>

Now the second and last step create New Block file and named it “PluginBtnOrderView.php” at this following path using below code.
app\code\Vendor\Extension\Plugin\PluginBtnOrderView.php

<?php

namespace Vendor\Extension\Plugin;

use Magento\Backend\Model\UrlInterface;
use Magento\Framework\ObjectManagerInterface;

class PluginBtnOrderView
{
    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' => __('Custom BUTTON SMS'),
                'onclick' => "setLocation('" . $ sendOrder. "')",
                'class' => 'ship primary'
            ]
        );

        return null;
    }

}

That’s it. Simply clear cache and you will able to find your custom button under the hood of sales order view section in your Magento 2 backend.
Let us know if you are facing an issue while implementing using this code by commenting below.
Happy Coding!

Click to rate this post!
[Total: 16 Average: 4.1]
Dhiren Vasoya

Dhiren Vasoya is a Director and Co-founder at MageComp, Passionate 🎖️ Certified Magento Developer👨‍💻. He has more than 9 years of experience in Magento Development and completed 850+ projects to solve the most important E-commerce challenges. He is fond❤️ of coding and if he is not busy developing then you can find him at the cricket ground, hitting boundaries.🏏

View Comments

  • Hii,
    I Use this code but in my case, the button redirects to the admin dashboard page.
    So can Please help me with this.
    Thank you.

    • You have to pass the proper URL here : 'onclick' => "setLocation('" . $ sendOrder. "')",
      onclicking of this button where you want to take user.

Recent Posts

How to Integrate ChatGPT with Laravel Application?

In this guide, we'll explore how to integrate ChatGPT, an AI-powered chatbot, with a Laravel…

13 hours ago

What are Net Sales? How to Calculate Your Net Sales?

In the world of business, understanding financial metrics is crucial for making informed decisions and…

3 days ago

Magento 2 Extensions Digest April 2024 (New Release & Updates)

Welcome to the MageComp Monthly Digest, where we bring you the latest updates, releases, and…

3 days ago

The ABCs of Geofencing: Definition, Features and Uses

In this era, businesses are always on the lookout for ways to engage with their…

4 days ago

How to Delete Product Variant in a Shopify Remix App using GraphQL Mutations?

Managing a Shopify store efficiently involves keeping your product catalog organized. This includes removing outdated…

4 days ago

6 Innovative Tools Revolutionizing E-Commerce Operations

E-commerce has transformed the way consumers shop for products and services and interact with businesses.…

7 days ago