Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 812
silex Avatar asked Jul 10 '11 17:07

silex


3 Answers

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.

like image 70
Matthias Kleine Avatar answered Oct 10 '22 19:10

Matthias Kleine


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');
    }
}
like image 39
silex Avatar answered Oct 10 '22 20:10

silex


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();

    }
}
like image 33
Tom Avatar answered Oct 10 '22 20:10

Tom