How to add new button to order view in Magento adm

2019-03-09 09:01发布

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

4条回答
Emotional °昔
2楼-- · 2019-03-09 09:31

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');
    }
}
查看更多
干净又极端
3楼-- · 2019-03-09 09:38

If you want to do it quick-and-dirty (i.e. editing core files), open app/code/core/Mage/Adminhtml/Block/Sales/Order/View.php and add something like:

    $this->_addButton('order_reorder', array(
        'label'     => Mage::helper('sales')->__('Print Labels'),
        'onclick'   => 'window.open(\'/printouts/' . $this->getOrder()->getRealOrderId() . '.pdf\')',
    ));

You can place that before this block:

    if ($this->_isAllowedAction('emails') && !$order->isCanceled()) {
        $message = Mage::helper('sales')->__('Are you sure you want to send order email to customer?');
        $this->addButton('send_notification', array(
            'label'     => Mage::helper('sales')->__('Send Email'),
            'onclick'   => "confirmSetLocation('{$message}', '{$this->getEmailUrl()}')",
        ));
    }

Your challenge, should you choose to accept is to create a file in local that is an over-ride of the core file, and to post it here!

查看更多
不美不萌又怎样
4楼-- · 2019-03-09 09:50

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

    }
}
查看更多
男人必须洒脱
5楼-- · 2019-03-09 09:53

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.

查看更多
登录 后发表回答