How to add custom button to order view page near "Back" and "Edit"?
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.
config.xml:
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_view>Namespace_Module_Block_Adminhtml_Sales_Order_View</sales_order_view>
</rewrite>
</adminhtml>
</blocks>
</global>
Namespace/Module/Block/Adminhtml/Sales/Order/View.php:
class Namespace_Module_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
public function __construct() {
parent::__construct();
$this->_addButton('button_id', array(
'label' => Mage::helper('xxx')->__('Some action'),
'onclick' => 'jsfunction(this.id)',
'class' => 'go'
), 0, 100, 'header', 'header');
}
}
In reference to the comments above about the parent::__constructor, here is what worked for me:
class Name_Module_Block_Adminhtml_Sales_Order_View extends Mage_Adminhtml_Block_Sales_Order_View {
public function __construct() {
$this->_addButton('testbutton', array(
'label' => Mage::helper('Sales')->__('Toms Button'),
'onclick' => 'jsfunction(this.id)',
'class' => 'go'
), 0, 100, 'header', 'header');
parent::__construct();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With