Adding a Print Button to Magento Order View Page
Hello developers! I hope you’re doing well. Today, we’ll tackle an interesting question about adding a custom print button for orders in the Magento admin panel. Let’s dive right in!
Understanding the Current Setup
The current setup involves:
- An old Magento instance with a print button on the order detail page.
- A new Magento installation where the template hints don’t show the button.
- A well-structured custom module configured correctly.
However, the custom button we’re trying to add isn’t appearing as expected.
Identifying the Issue
The primary issue lies in the Observer.php file for the custom module:
class CaitlinHavener_printOrder_Model_Observer
{
public function orderPageButton( Varien_Event_Observer $observer )
{
if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
$block->addButton('test_print', array(
'label' => 'Test',
'onclick' => 'setLocation(\'' . $block->getUrl('html/sales_order/print') . '\')',
'class' => 'go'
));
}
}
}
There are a few problems here:
- The
$blockvariable is not defined anywhere in the function. - The
setLocation()function might not work as expected in this context.
Solving the Problem
To fix these issues, we need to make some changes:
- Define the $block variable: We can get the current block instance using the observer’s event object:
public function orderPageButton( Varien_Event_Observer $observer )
{
$event = $observer->getEvent();
$block = $event->getBlock();
if(get_class($block) =='Mage_Adminhtml_Block_Sales_Order_View'
&& $block->getRequest()->getControllerName() == 'sales_order')
{
// Rest of the code
}
}
- Use the correct URL for the print action: The
setLocation()function might not work with thehtml/sales_order/printroute in Magento 2. Instead, we should use the appropriate sales order view URL.
'onclick' => 'window.open(\'' . $block->getUrl('sales/order/view', ['_current' => true, '_query' => ['print' => 1]]) . '\')',
Implementation Steps
Here’s how to implement this:
- Update your
Observer.phpfile with the changes mentioned above. - Ensure that the
config.xmlandCaitlinHavener_printOrder.xmlfiles are properly configured as shown in the question. - Clear all Magento caches.
- Flush the Magento cache storage.
Conclusion
Adding custom buttons to the Magento admin panel involves a few key steps:
- Creating an observer for the appropriate event (in this case,
core_block_abstract_to_html_before). - Defining the button’s attributes correctly in the observer method.
- Making sure the button appears only on the desired page.
Remember to flush your cache after making changes to ensure they take effect.
If you’re still having issues, feel free to reach out! I’d be happy to help you further troubleshoot this problem.
For more Magento tips and tricks, don’t forget to check out our Magento Development category on the Laravel Company blog. Happy coding!
Thank you for your time, and I hope this solution helps! Let me know if you have any questions or need further clarification.
Best,
[Your Name] Senior Developer & Technical Blogger at Laravel Company