How to add new button to order view in Magento admin panel?

silex picture silex · Jul 10, 2011 · Viewed 30.2k times · Source

How to add custom button to order view page near "Back" and "Edit"?

Answer

Matthias Kleine picture Matthias Kleine · Aug 29, 2014

Instead of core hacks or rewrites, just use an observer to add the button to the order:

<adminhtml>
    <events>
        <adminhtml_widget_container_html_before>
            <observers>
                <your_module>
                    <class>your_module/observer</class>
                    <type>singleton</type>
                    <method>adminhtmlWidgetContainerHtmlBefore</method>
                </your_module>
            </observers>
        </adminhtml_widget_container_html_before>
    </events>
</adminhtml>

Then just check in the observer if the type of the block matches the order view:

public function adminhtmlWidgetContainerHtmlBefore($event)
{
    $block = $event->getBlock();

    if ($block instanceof Mage_Adminhtml_Block_Sales_Order_View) {
        $message = Mage::helper('your_module')->__('Are you sure you want to do this?');
        $block->addButton('do_something_crazy', array(
            'label'     => Mage::helper('your_module')->__('Export Order'),
            'onclick'   => "confirmSetLocation('{$message}', '{$block->getUrl('*/yourmodule/crazy')}')",
            'class'     => 'go'
        ));
    }
}

The "getUrl" function of the block will automatically append the current order id to the controller call.