我想在Order for s Grid for Admin中添加一个额外的列。假设其客户组ID。
我的app / etc / modules / MyProject_Adminhtml看起来像是:
<?xml version="1.0"?>
<config>
<modules>
<MyProject_Adminhtml>
<active>true</active>
<codePool>local</codePool>
<depends>
<Mage_Sales />
</depends>
</MyProject_Adminhtml>
</modules>
</config>
我的app / code / local / MyProject / Adminhtml / etc / config.xml如下所示:
<?xml version="1.0"?>
<config>
<modules>
<MyProject_Adminhtml>
<version>1.0.0</version>
</MyProject_Adminhtml>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<sales_order_grid>MyProject_Adminhtml_Block_Sales_Order_Grid</sales_order_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
在app / code / local / MyProject / Adminhtml / Block / Sales / Order / Grid.php中我覆盖了Mage_Adminhtml_Block_Sales_Order_Grid
class MyProject_Adminhtml_Block_Sales_Order_Grid extends Mage_Adminhtml_Block_Sales_Order_Grid
{
protected function _prepareColumns()
{
.... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...
$this->addColumn('customer_group_id', array(
'header' => Mage::helper('sales')->__('Customer Group Id'),
'index' => 'customer_group_id',
'type' => 'text',
));
.... unchanged code from Mage_Adminhtml_Block_Sales_Order_Grid::_prepareColumns ...
}
}
我是否遗漏了一些东西,因为我在订单网格中没有看到任何内容。我正在使用Magento 1.4.1.1
在Anda B的评论中,我写了以下一行:
var_dump($this->getLayout()->createBlock('MyProject_Adminhtml_Block_Sales_Order_Grid'));
在app / code / core / Mage / Adminhtml / controllers / Sales / Order / CreateController.php
然后,我选择了“创建新订单”并取消订单以查看var_dump的执行结果,我看到以下内容:
/var/www/magento/var/report/72990635: line 10: syntax error near unexpected token `}' /var/www/magento/var/report/72990635: line 10: `#9 {main}";s:3:"url";s:80:"/index.php/admin/sales_order_create/cancel/key/0624033594dd63d9e145fc538f4c6bbb/";s:11:"script_name";s:10:"/index.php";s:4:"skin";s:5:"admin";}'
答案 0 :(得分:4)
你快到了。
您需要为GroupID创建一个渲染器,因为核心中不存在渲染器。
首先,将渲染器添加到addColumn,如下所示:
$this->addColumn('customer_group_id', array(
'header' => Mage::helper('sales')->__('Customer Group Id'),
'index' => 'customer_group_id',
'type' => 'text',
'renderer' => 'adminhtml/widget_grid_column_renderer_customergroup',
));
现在您需要创建目录/ app / code / local / MyProject / Adminhtml / Widget / Grid / Column / Renderer /,因为它可能不存在。
现在创建一个包含此类的Customergroup.php文件:
class MyProject_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup extends Mage_Adminhtml_Block_Widget_Grid_Column_Renderer_Abstract
{
private static $_customergroups = array();
public static function getCustomerGroupsArray() {
if(count(self::$_customergroups) == 0) {
$customer_group = Mage::getModel('customer/group');
$customer_groups = $customer_group->getCollection()->toOptionHash();
self::$_customergroups = $customer_groups;
}
return self::$_customergroups;
}
public function render(Varien_Object $row){
$value = $this->_getValue($row);
$customer_groups = self::getCustomerGroupsArray();
return isset($customer_groups[$value]) ? $customer_groups[$value] : false;
}
}
最后,您需要将其添加到MyProject中的config.xml中。把这个:
<widget_grid_column_renderer_customergroup>Myproject_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup</widget_grid_column_renderer_customergroup>
在你的其他重写旁边。
刷新缓存后,您的销售订单网格中应该有您的组标签。
PS。
如果要在销售订单网格的顶部添加过滤器以使用此列,请在呈现器后面的“addColumn”中添加此过滤器。 (订单实际上并不重要)
'options' => TheReadyStore_Adminhtml_Block_Widget_Grid_Column_Renderer_Customergroup::getCustomerGroupsArray(),
并将类型从“text”更改为“options”
干杯 罗伊
答案 1 :(得分:1)
在模块/ etc中你有Atzen_Adminhtml,但你的项目是MyProject_Adminhtml。 除了这个问题,代码应该可以工作:即使您在sales表中没有customer_group_id,新列也应该出现在网格中。