Magento: Adding a Custom Tab to the Admin Order Details Page
Introduction
In this post, we’ll guide you through adding a custom tab to the admin order details page in Magento. We’ll assume that you’ve already created a custom module with some user input data associated with orders.
Current Approach and Challenges
The developer has taken a good initial approach but is facing a 404 error when clicking on the new tab:
- The
config.xmlfile correctly overrides the adminhtml router. - The necessary blocks and layout updates are defined in the module’s XML files.
- The custom block extends Magento’s built-in order view tab block.
However, there are two key issues:
- The
viewAction()method of the overridden controller doesn’t get called when viewing an order detail page. - The custom
attackAction()method isn’t triggered when clicking the new tab.
Identifying the Problem: Controller Override
The problem lies in how Magento handles adminhtml controller overrides. By default, Magento uses a single instance of the Mage_Core_Controller_Varien_Router_Admin class for all adminhtml requests. This means that even though you’ve overridden the sales_order router, it’s still using the same controller instance as other adminhtml controllers.
The Correct Approach: Using an Additional Router
To fix this issue, we need to create a separate router for our custom module. Here’s how:
- In your module’s
config.xml, add a new router inside the<adminhtml>node:
<admin>
<routers>
<attack>
<use>standard</use>
<args>
<module>Zac_Attack</module>
<frontName>attack</frontName>
</args>
</attack>
</routers>
</admin>
-
Ensure that the front name
attackis unique and not used by any other module. -
In your controller file (
Adminhtml/Sales/OrderController.php), change the namespace to match the new router name:
namespace Zac\Attack\Controller\Adminhtml\Sales;
class Order extends \Magento\Framework\App\Action\Action
{
// Your code here...
}
- Update the
dispatch()method in your module’setc/frontend.xmlfile to use the new router:
<frontend>
<routers>
<attack>
<use>standard</use>
<args>
<module>Zac_Attack</module>
<frontName>attack</frontName>
</args>
</attack>
</routers>
</frontend>
Final Steps and Best Practices
-
Clear your cache after making these changes.
-
Ensure that the controller class name matches the new router configuration exactly, including namespace.
-
Remember to add any necessary dependencies or injections in your constructor using Magento’s dependency injection system.
-
Test the module thoroughly on an isolated development environment before deploying it to a live site.
Conclusion
In this post, we’ve identified and resolved the issue with adding a custom tab to the admin order details page in Magento. By understanding how Magento handles controller overrides and using additional routers for custom modules, we can achieve the desired functionality without running into 404 errors.
For more detailed information on developing Magento extensions, consider exploring Laravel Company’s official Magento development resources.
Remember to always follow security best practices when working with e-commerce platforms and keep your modules up-to-date with the latest Magento versions.
Happy coding!