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

时间:2011-07-10 17:21:14

标签: magento button admin

如何在“后退”和“编辑”附近添加自定义按钮以订购视图页?

4 个答案:

答案 0 :(得分:40)

只需使用观察者将按钮添加到订单中,而不是核心黑客或重写:

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

然后只需检查观察者是否类型与订单视图匹配:

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

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

答案 1 :(得分:24)

<强> config.xml中:

<global>
    <blocks>
         <adminhtml>
            <rewrite>
                <sales_order_view>Namespace_Module_Block_Adminhtml_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>
 </global>

<强>命名空间/模块/块/ Adminhtml /销售/订购/ 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');
    }
}

答案 2 :(得分:2)

参考上面关于parent :: __构造的注释,这里有什么对我有用:

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

    }
}

答案 3 :(得分:-2)

如果你想快速而肮脏(即编辑核心文件),请打开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()}')",
        ));
    }

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