How to add custom tab in admin Sales Order view magento 2?

In Magento 2, Sales Order Page in the admin panel, Many Tabs are available by native like Information, Invoices, Shipments, Credit Memos, Transactions and comment history.

For your custom requirement, you need to add extra tab in Order page, You can add your custom tab in Admin Order View page by just simple module.

You need to create simple module for add extra tab.

Refer below code snippet for add extra tab,
I have taken Rbj as Package name and OrderTab as Modulename for simplicity.
You need to create first registration.php and module.xml file for defining our module.

Path: app/code/Rbj/OrderTab/registration.php

Create module.xml file, Path: app/code/Rbj/OrderTab/etc/module.xml

We have added the dependency on Magento Sales Module to add the new tab. So we have defined Magento_Sales module in sequence tag in the above XML file.

Now comes to the main entry point of a module,
For Add new tab, We must override sales_order_view.xml file to add our logic of add custom tabs.

Reference block sales_order_tabs contains the tab lists. so we need to create sales_order_view.xml file in our module at below path,

Path: app/code/Rbj/OrderTab/view/adminhtml/layout/sales_order_view.xml

In the above file, We have declared Block file for set your custom logic, you want to display in custom tabs.

Create a New Block PHP file, Path: app/code/Rbj/OrderTab/Block/Adminhtml/OrderEdit/Tab/View.php

Above file, We can declare a template file using a $_template variable.

protected $_template = ‘tab/view/my_order_info.phtml’ used for template file for our custom tabs.

You can set Custom Tab label by getTabLabel() and set title using getTabTitle() function.You can define a custom function for your requirement in the above file.

You can get Current order data by calling Magento\Framework\Registry object.

Now we need to create template file,
Path: app/code/Rbj/OrderTab/view/adminhtml/templates/tab/view/my_order_info.phtml

Now Run Upgrade command to install our module.

php bin/magento setup:upgrade
php bin/magento cache:flush

Now Go To Admin panel, Login with your credentials,
Click on Left Sidebar, Sales -> Order Link,
Click on Any Order, You can get the last tab as your new custom tab in Order View page.

Check Custom tab in Sales Order Page,

Order-tab-sales-order-magento
Order-tab-sales-order-magento

Leave a Reply

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