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

sil*_*lex 24 admin button magento

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

Mat*_*ine 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>
Run Code Online (Sandbox Code Playgroud)

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

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'
        ));
    }
}
Run Code Online (Sandbox Code Playgroud)

块的"getUrl"函数将自动将当前订单ID附加到控制器调用.

  • 这是一个更好的答案.需要子类化来覆盖默认Magento类的答案会导致许多扩展冲突和升级不兼容问题.如果Magento有一个使用Observer的方法,通常就是这种方法. (2认同)

sil*_*lex 24

config.xml文件:

<global>
    <blocks>
         <adminhtml>
            <rewrite>
                <sales_order_view>Namespace_Module_Block_Adminhtml_Sales_Order_View</sales_order_view>
            </rewrite>
        </adminhtml>
    </blocks>
 </global>
Run Code Online (Sandbox Code Playgroud)

命名空间/模块/块/ 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');
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 'onclick'方法的一个示例是"confirmSetLocation('{$ message}','{$ this-> getOkToShipUrl()}')", (3认同)
  • 请使用观察者而不是在这样一个重要的核心类中添加重写.使用此解决方案,您可能会遇到其他扩展的问题,并且无需重写即可实现此目的! (3认同)