如何在Magento管理面板的订单视图中添加新按钮?

如何在Magento管理面板的订单视图中添加新按钮?,magento,button,admin,Magento,Button,Admin,如何将自定义按钮添加到“后退”和“编辑”附近的订单视图页面?如果您想快速而肮脏地完成此操作(即编辑核心文件),请打开app/code/core/Mage/Adminhtml/Block/Sales/order/view.php并添加以下内容: $this->_addButton('order_reorder', array( 'label' => Mage::helper('sales')->__('Print Labels'),

如何将自定义按钮添加到“后退”和“编辑”附近的订单视图页面?

如果您想快速而肮脏地完成此操作(即编辑核心文件),请打开
app/code/core/Mage/Adminhtml/Block/Sales/order/view.php并添加以下内容:

    $this->_addButton('order_reorder', array(
        'label'     => Mage::helper('sales')->__('Print Labels'),
        'onclick'   => 'window.open(\'/printouts/' . $this->getOrder()->getRealOrderId() . '.pdf\')',
    ));
您可以将其放置在此块之前:

    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()}')",
        ));
    }

如果您选择接受,您的挑战是在本地创建一个文件,该文件是核心文件的替代文件,并将其发布到此处

config.xml:

<global>
    <blocks>
         <adminhtml>
            <rewrite>
                <sales_order_view>Namespace_Module_Block_Adminhtml_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>
 </global>
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');
    }
}

参考上面关于父构造函数的评论::_,以下是对我有效的方法:

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

    }
}

不要进行核心破解或重写,只需使用观察者将按钮添加到订单:

<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>

块的“getUrl”函数将自动将当前订单id附加到控制器调用。

onclick方法的一个示例是“confirmSetLocation(“{$message}”,“{$this->getOkToShipUrl()}”),需要调用父对象::u construct();在您的自定义_construct()函数中,否则我将得到“无效块类型”异常。我在实现此功能时出错@james你所说的父::_构造是什么意思?我刚刚明白了,对于其他人来说,我在下面添加了它作为答案。请使用一个观察者,而不是对如此重要的核心类进行重写。使用此解决方案,您可能会遇到其他扩展的问题,而实现此目的不需要重写!您应该执行类似$return=parent::_construct();在函数开始时,执行您的操作,然后返回$return;最后,@GabrielQueirozSilva构造函数没有返回值。这是一个正确的答案。@MatthiasKleine ops并没有注意到它是一个构造函数。最糟糕的答案-在任何情况下都不要修改核心文件。从未。容易的。没有快速和肮脏-保持干净的编码!没有类重写,没有corefile更改-非常棒的解决方案,我可以确认它至少在1.8中可以工作。这是一个更好的答案。像这样需要子类化来覆盖默认Magento类的答案将导致许多扩展冲突和升级不兼容问题。如果Magento有一种使用观察者的方法,通常就是这种方法。
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'
        ));
    }
}