Adding Custom Tabs to Magento Sales Order Views Without Modifying Core Code
As an experienced Magento developer, I often get asked how to add new tabs to sales order views without touching the core files. This is a valid concern, as modifying core files can lead to issues during updates and make your customizations difficult to maintain.
In this post, we’ll explore a clean approach that uses extension points provided by Magento instead of directly editing core templates or layouts.
Understanding Magento’s Extension Points
Magento offers several extension points that allow you to add new functionality without touching the core codebase. These include layout updates, template overrides, and view files within your custom theme or module.
The key directories for these extensions are:
- /app/design/frontend/{Vendor}/{Theme}/template
- /app/design/frontend/{Vendor}/{Theme}/layout
These directories are part of the Magento file structure allocated for customizations by third-party packages. They allow you to override core files without directly modifying them, ensuring your changes persist across updates.
Creating a Custom Order View Tab
To add a new tab to the sales order view, follow these steps:
-
Create a custom module if you haven’t already. You can use the Magento Marketplace guide for setting up a basic module: https://laravelcompany.com/magento-blog-module-setup-guide
-
In your module’s /view/adminhtml/layout folder, create a new file named
sales_order_view.xml. -
Add the following XML code to define a new tab:
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout.xsd">
<update handle="sales_order_view"/>
<referenceBlock name="order.tabs">
<block class="Magento\Backend\Block\Widget\Tabs\Tab" as="mycustomtab" template="Magento_Backend::widget/tabs/tab.phtml">
<action method="addTab">
<argument name="name" translate="true">My Custom Tab</argument>
<argument name="block" xsi:type="string">YOURMODULENAME::order_view_mycustomtab</argument>
</action>
</block>
</referenceBlock>
</layout>
Replace YOURMODULENAME with the actual namespace of your module.
- Create a new XML layout file in /view/adminhtml/layout for the tab content:
<!-- app/code/YourVendor/YourModule/view/adminhtml/layout/order_view_mycustomtab.xml -->
<?xml version="1.0"?>
<layout xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/layout.xsd">
<referenceBlock name="order.tabs.content">
<block class="Magento\Framework\View\Element\Template" template="YourVendor_YourModule::mycustomtab.phtml"/>
</referenceBlock>
</layout>
- Create the PHTML template file for your tab content:
<!-- app/code/YourVendor/YourModule/view/adminhtml/templates/mycustomtab.phtml -->
<?php $block = $this->getLayout()->createBlock('Magento\Framework\View\Element\Template') ?>
<div class="admin__content">
<div class="admin__content-wrapper">
<h1>Welcome to My Custom Tab</h1>
<!-- Add your custom content or logic here -->
</div>
</div>
Conclusion
In this post, we demonstrated how to add a new tab to the Magento sales order view using extension points provided by the platform. By following this approach, you avoid modifying core files and ensure that your changes remain persistent across updates.
Remember, using the right directories for customizations is crucial for maintainability in Magento projects. Always prioritize using /app/design or /app/code directories over directly editing core files.
At Laravel Company, we specialize in providing high-quality Magento development services that employ best practices like these to deliver robust and sustainable e-commerce solutions.
If you have any questions about implementing this method or need assistance with other Magento customizations, feel free to reach out. We’d be happy to help you navigate the complexities of Magento while ensuring your project remains secure and performant.