如何在订单查看页面附近的“返回”和“编辑”按钮旁添加自定义按钮?
不要使用核心代码的黑客或重写,只需使用观察者将按钮添加到订单中:
<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 添加到控制器调用中。
config.xml:
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_view>Namespace_Module_Block_Adminhtml_Sales_Order_View</sales_order_view>
</rewrite>
</adminhtml>
</blocks>
</global>
名称空间/模块/块/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');
}
}
关于上面有关parent::__constructor的评论,这是对我有效的解决方法:
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();
}
}
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()}')",
));
}
如果您选择接受挑战,您需要在本地创建一个覆盖核心文件的文件,并将其发布在此处!