尝试在Magento v1.11中向管理客户网格添加具有自定义属性的列
我已经设置了这个模块:
config.xml中
<?xml version="1.0"?>
<config>
<modules>
<WACI_AdminHtmlExt>
<version>0.1.0</version>
</WACI_AdminHtmlExt>
</modules>
<global>
<blocks>
<adminhtml>
<rewrite>
<customer_grid>WACI_AdminHtmlExt_Block_Customer_Grid</customer_grid>
</rewrite>
</adminhtml>
</blocks>
</global>
</config>
WACI / AdminHtmlExt /砌块/客户/ Grid.php
<?php
/**
* Adminhtml customer grid block
*
* @category WACI
* @package WACI_AdminhtmlExt
* @author
*/
class WACI_AdminHtmlExt_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->addAttributeToSelect('customer_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
$this->addColumn('customer_id', array(
'header' => Mage::helper('customer')->__('Dynamics ID'),
'width' => '75px',
'index' => 'customer_id',
));
//... rest of the function, removing a couple columns...
return parent::_prepareColumns();
}
}
Customer_Id ,在这种情况下是自定义属性(跟踪内部客户ID)...不确定是否需要添加逻辑才能正确呈现此内容?但它在管理员中出现就好了,否则。
我读过一些文章,提到在这样的网格中为新字段添加渲染器 - 但正如许多人根本没有提到它一样。
不太确定从哪里开始 -
干杯
只是为需要此解决方案的人澄清:
class WACI_AdminHtmlExt_Block_Customer_Grid extends Mage_Adminhtml_Block_Customer_Grid
{
/*protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->addAttributeToSelect('customer_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
$this->setCollection($collection);
return parent::_prepareCollection();
}*/
public function setCollection($collection)
{
$collection->addAttributeToSelect('customer_id');
parent::setCollection($collection);
}
protected function _prepareColumns()
{
$this->addColumn('customer_id', array(
'header' => Mage::helper('customer')->__('Dynamics ID'),
'width' => '75px',
'index' => 'customer_id',
));
$this->addColumnsOrder('customer_id','entity_id');
parent::_prepareColumns();
$this->removeColumn('billing_country_id');
return $this;
}
}
是我最终落到了什么地方。跳过了_prepareCollenctions()
来电。
干杯
答案 0 :(得分:2)
问题是你的return语句调用parent :: _ prepareCollection()和你正在扩展原始类的事实。通过在类之后调用父类,您将替换使用原始类创建的集合对象。你真正需要调用的是你重载的类的父类,你可以这样做......
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('group_id')
->addAttributeToSelect('customer_id')
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
->joinAttribute('billing_city', 'customer_address/city', 'default_billing', null, 'left')
->joinAttribute('billing_telephone', 'customer_address/telephone', 'default_billing', null, 'left')
->joinAttribute('billing_region', 'customer_address/region', 'default_billing', null, 'left')
->joinAttribute('billing_country_id', 'customer_address/country_id', 'default_billing', null, 'left');
$this->setCollection($collection);
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
答案 1 :(得分:0)
对不起错误的代码阻止因为我第一次回答时的换行符。
您的createBlock()
方法似乎没有返回有效对象。