---
title: "How to Add Custom Button to Admin Sales Invoice View in Magento 2"
url: "https://dolphinwebsolution.com/add-custom-button-to-admin-sales-invoice-view-in-magento-2"
date: "2021-09-28T06:36:37+05:30"
modified: "2023-08-25T09:43:20+05:30"
author:
  name: "Jigar Patel"
categories:
  - "Magento"
word_count: 593
reading_time: "3 min read"
summary: "Would you like to add custom button to admin sales invoice view? Let\'s do how you can easily do that by creating a Magento 2 Extension! The best way to do that is by creating a Magento 2 plugin to ..."
description: "In this article, Today I will instruct on How to Add Custom Button to Admin Sales Invoice View in Magento 2. Let\'s do how you can easily do that by creating ..."
keywords: "Add Custom Button to Admin Sales,admin sales invoice view,admin sales invoice view in magento 2,add custom button in magento 2, Magento"
language: "en"
schema_type: "Article"
related_posts:
  - title: "How to Set Custom Validation Message in Magento 2"
    url: "https://dolphinwebsolution.com/set-custom-validation-message-in-magento-2"
  - title: "Magento 2 Display Image in Admin UI Grid"
    url: "https://dolphinwebsolution.com/magento-2-display-image-in-admin-ui-component-grid"
  - title: "How To Programmatically Export Category Name With Parent Category Name Magento 2"
    url: "https://dolphinwebsolution.com/how-to-programmatically-export-category-name-with-parent-category-name-magento-2"
---

# How to Add Custom Button to Admin Sales Invoice View in Magento 2

_Published: September 28, 2021_  
_Author: Jigar Patel_  

![](https://dolphinwebsolution.com/wp-content/uploads/2023/02/How-to-Add-Custom-Button-to-Admin-Sales-Invoice-View-in-Magento-2-1024x536.png)

Would you like to **add custom button to admin sales invoice view?** Let’s do how you can easily do that by creating a [Magento 2 **Extension**](https://dolphinwebsolution.com/shop/magento-2-extensions.html)! The best way to do that is by creating a Magento 2 plugin to extend the behavior of the public function **setLayout** of the class.

The plugin or Interceptor is a class the modifies the behavior of the public class method by interceptions. The plugin gives users three options to use **After methods, Before methods and Around Method**.

## How to Add Custom Button to Admin Sales Invoice View in Magento 2
Each component will have its own custom function depending on the purpose of each person. We will introduce several ways to add a new element to order view, code snippets are under a module named **Dolphin\_Menu**. We start by using a plugin targeting **‘beforeSetLayout’**.

We are already learned [how to create a basic module in Magento 2](https://dolphinwebsolution.com/how-to-create-module-in-magento-2). We need to create **module.xml** and **registration.php** file. You can create **module.xml** and **registration.php** from this tutorial.

### Step 1: Create Dependency injection (di.xml) under the adminhtml scope,
Now we create Dependency injection file **di.xml** in **app/code/Dolphin/InvoiceEmail/etc/adminhtml** with the following code.

```
<?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\Sales\Block\Adminhtml\Order\Invoice\View">
        <plugin name="Dolphin_InvoiceEmail_Order_View" type="Dolphin\InvoiceEmail\Plugin\Adminhtml\Order\Invoice\View" sortOrder="10" disabled="false"/>
    </type>
</config>
```

**View class** to add button and its attributes

[Hire Custom Magento 2 Extension Developer](https://dolphinwebsolution.com/hire-magento-2-developers/)

### Step 2: Create plugin class View.php
Finally, add in the Plugin class. As with the default invoice view plugin, a beforeSetLayout() method is defined, and the button is added as part of the \\Magento\\Sales\\Block\\Adminhtml\\Order\\Invoice\\View class.

Create plugin class file **View.php** in **app/code/Dolphin/InvoiceEmail/Plugin/Adminhtml/Order/Invoice** folder with the following code.

```
<?php
namespace Dolphin\InvoiceEmail\Plugin\Adminhtml\Order\Invoice;

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

class View
{
    protected $object_manager;
    protected $_backendUrl;

    public function __construct(
        \Magento\Backend\Block\Widget\Context $context,
        ObjectManagerInterface $om,
        UrlInterface $backendUrl,
        array $data = []
    ) {
        $this->object_manager = $om;
        $this->_backendUrl = $backendUrl;
    }

    public function beforeSetLayout(\Magento\Sales\Block\Adminhtml\Order\Invoice\View $view)
    {
        if ($view->getInvoice()->getId()) {
            $url = $this->_backendUrl->getUrl('dolphininvoice/email/send/invoice_id/' . $view->getInvoice()->getId());
            $view->addButton(
                'order_send_email',
                [
                    'label' => __('Send Email'),
                    'class' => 'custom-email-send primary',
                    'onclick' => 'setLocation(\'' . $url . '\')',
                ]
            );
            //$view->removeButton('send_notification');
        }

    }

}
```

- **$view->addButton:** Selector for the element used for item adding.
- \[dt\_code\]addButton($buttonId, $data, $level = 0, $sortOrder = 0, $region = ‘toolbar’)\[/dt\_code\]
- ****$buttonId:**** Unique id for button ****(string)**** ex: order\_send\_email
- ****$data:**** You can add button label ,class, type ,onclick, etc.. **(array)**
- **$level:** Default level is 0.You can add level of button. **(integer)**
- **$sortOrder:** Default sort order is 0.You can add sort order of button. **(integer)**
- **$region:** That button should be displayed in (‘toolbar’, ‘header’, ‘footer’, null) **(string|null)**
- **$view->removeButton:** Selector for the element used for item removing.
- \[dt\_code\]removeButton($buttonId)\[/dt\_code\]
- **$buttonId:** Unique id for button (string).Pass button id you remove it.

**Result:**

Clear cache and you will be able to find your custom button under the sales invoice view section in your Magento 2 backend.

![AI Integration in Magento: Benefits, Use Cases and Business Impact](https://dolphinwebsolution.com/wp-content/uploads/2021/09/invoice-custom-button-1024x399.png)

If you have any queries regarding this post, use the comment section below!

**Happy Coding!**


---

_View the original post at: [https://dolphinwebsolution.com/add-custom-button-to-admin-sales-invoice-view-in-magento-2](https://dolphinwebsolution.com/add-custom-button-to-admin-sales-invoice-view-in-magento-2)_  
_Served as markdown by [Third Audience](https://github.com/third-audience) v3.5.3_  
_Generated: 2026-08-26 07:39:02 UTC_  
