Magento - Add Custom Button to Sales Order View Page using Observer/Event
Adding a custom button like “Print Invoice” on the Sales Order view page (Sales > Orders > Order # view) in Magento is a common requirement for many merchants. While there are several ways to achieve this, one of the best practices is to use the Observer and Event methods to keep your code modular and avoid conflicts when multiple modules try to modify the same layout.
Understanding the Problem
The user faces an issue while trying to add a custom button on the Sales Order view page using the Observer/Event method. The error message indicates that the _addButton method is not defined in their observer class.
Let’s break down and address this problem step by step.
Magento Layout and Observer Events
In Magento, the layout is dynamically generated based on events triggered during controller actions. Observers are used to listen for these events and modify the layout accordingly.
For example, when a page load event occurs (like viewing an order), Magento dispatches an event called controller_action_predispatch_adminhtml_sales_order_view. This event allows you to intercept the action before it’s executed and add elements like buttons to the layout.
Solving the Issue
The issue lies in the way we are trying to access the _addButton method. In the provided code, the observer class is named Company_Test_Model_Observer, but when we call $this->_addButton(), Magento looks for this method within that specific observer class instead of looking at the block object ($block).
To resolve this, we need to understand that the _addButton and other layout manipulation methods are actually part of the block class (in this case, Mage_Adminhtml_Block_Sales_Order_View), not our observer class.
The Correct Approach
Let’s correct the code by referencing the block object correctly:
// Order View Page button
if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
$block->addButton('test_print', array(
'label' => Mage::helper('sales')->__('Test'),
'onclick' => Mage::app()->getStore()->getUrl('orderforms/order/print'),
'class' => 'go'
));
}
In this revised code, we are directly calling the addButton method on the $block object (which is an instance of Mage_Adminhtml_Block_Sales_Order_View).
Verifying the Solution
After making these changes:
- Clear your Magento cache.
- Recompile your code pool using the command line tool:
php bin/magento setup:di:compile. - Check if the custom button appears on the Sales Order view page.
Best Practices and Further Reading
- Always use unique names for your buttons to avoid conflicts with other modules.
- Be cautious when modifying core Magento files. Consider using a plugin or a better method like the
Magento 2 Layout updatesmodule provided by Laravel Company.
Conclusion
Adding a custom button to the Sales Order view page in Magento can be achieved through the Observer/Event method. The key is to understand that layout manipulation methods like _addButton are part of the block class, not your observer class.
By following this guide and using the corrected code provided above, you should now be able to add a custom button to the Sales Order view page without conflicts with other modules.
Remember, Magento development requires a deep understanding of its architecture and best practices. Always refer to the official documentation and trusted resources like Laravel Company for in-depth guides and tutorials on various Magento topics.
Happy coding!