我做过同样的事情。按顺序添加了“ order_type”字段,并将其显示在网格中。它在Magento 1.7.0.2版中运行良好
如何在管理员的销售订单网格中添加订单类型字段?
1)我们必须使用以下代码创建一个安装sql文件。
<?php
/* @var $installer Mage_Sales_Model_Entity_Setup */
$installer = $this;
$installer->startSetup();
$installer->run("
ALTER TABLE `{$installer->getTable('sales/order')}` ADD `order_type` VARCHAR(255) NOT NULL;
ALTER TABLE `{$installer->getTable('sales/order_grid')}` ADD `order_type` VARCHAR(255) NOT NULL;
ALTER TABLE `{$installer->getTable('sales/quote')}` ADD `order_type` VARCHAR(255) NOT NULL;
");
$installer->endSetup();
?>
2)覆盖Mage_Adminhtml_Block_Sales_Order_Grid文件,并在其中添加以下代码。
<?php
class Mycompany_Mymodule_Block_Adminhtml_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareColumns()
{
$options = array(
'ordertypeID1' => 'order type label1',
'ordertypeID2' => 'order type label2',
'ordertypeID3' => 'order type label3',
);
$this->addColumn('order_type', array(
'header' => Mage::helper('customer')->__('Order Type'),
'width' => '100',
'index' => 'order_type',
'type' => 'options',
'options' => $options
));
$this->addColumnsOrder('order_type', 'grand_total');
return parent::_prepareColumns();
}
}
?>
3)创建一个观察者事件以添加/更新订单类型字段值
打开你的模块/etc/config.xml
<config>
<adminhtml>
<events>
<adminhtml_sales_order_create_process_data>
<observers>
<modulename>
<class>Mycompany_Mymodule_Model_Adminhtml_Observer</class>
<method>adminhtml_sales_order_create_process_data</method>
</modulename>
</observers>
</adminhtml_sales_order_create_process_data>
<sales_convert_quote_to_order>
<observers>
<modulename>
<type>model</type>
<class>Mycompany_Mymodule_Model_Adminhtml_Observer</class>
<method>sales_convert_quote_to_order</method>
</modulename>
</observers>
</sales_convert_quote_to_order>
</events>
</adminhtml>
</config>
4)创建一个类Mycompany_Mymodule_Model_Adminhtml_Observer的观察者文件
<?php
class Mycompany_Mymodule_Model_Adminhtml_Observer
{
public function adminhtml_sales_order_create_process_data(Varien_Event_Observer $observer)
{
try {
$requestData = $observer->getEvent()->getRequest();
if (isset($requestData['order']['order_type'])) {
$observer->getEvent()->getOrderCreateModel()->getQuote()
->addData($requestData['order'])
->save();
}
} catch (Exception $e) {
Mage::logException($e);
}
return $this;
}
/**
*
* @param Varien_Event_Observer $observer
* @return Mycompany_Mymodule_Model_Adminhtml_Observer
*/
public function sales_convert_quote_to_order(Varien_Event_Observer $observer)
{
if ($ordertype = $observer->getEvent()->getQuote()->getOrderType()) {
try {
$observer->getEvent()->getOrder()
->setOrderType($ordertype);
} catch (Exception $e) {
Mage::logException($e);
}
}
return $this;
}
}
?>