我创建了一个自定义客户属性:
$installer->addAttribute('customer', 'forum_username', array(
'type' => 'varchar',
'input' => 'text',
'label' => 'Forum username',
'global' => 1,
'visible' => 1,
'required' => 0,
'user_defined' => 1,
'default' => '',
'visible_on_front' => 1,
));
现在我想在客户网格上展示它 我已经扩展了Mage_Adminhtml_Block_Customer_Grid并添加了一列:
protected function _prepareColumns()
{
$this->addColumnAfter('forum_username', array(
'header' => Mage::helper('customization')->__('Forum user name'),
'index' => 'forum_username',
'type' => 'text'
),'email');
return parent::_prepareColumns();
}
列显示在网格中,但我无法获取属性值,我不明白如何使用joinAttribute()获取值以获取邮政编码:
->joinAttribute('billing_postcode', 'customer_address/postcode', 'default_billing', null, 'left')
我试图了解它在此页面上的工作原理: http://magentocommerces.wordpress.com/2012/04/17/addattributetosortaddexpressionattributetoselect-joinattribute/ 并来了:
->joinAttribute('forum_username', 'customer/forum_username', 'attribute_set_id', null, 'left')
但它不起作用
答案 0 :(得分:1)
Never84,请添加此代码addAttributeToSelect('forum_username')
,网格如下 -
protected function _prepareCollection()
{
$collection = Mage::getResourceModel('customer/customer_collection')
->addNameToSelect()
->addAttributeToSelect('email')
->addAttributeToSelect('created_at')
->addAttributeToSelect('forum_username')
->addAttributeToSelect('group_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();
return Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
}
protected function _prepareColumns()
{
$this->addColumn('entity_id', array(
'header' => Mage::helper('customer')->__('ID'),
'width' => '50px',
'index' => 'entity_id',
'type' => 'number',
));
/*$this->addColumn('firstname', array(
'header' => Mage::helper('customer')->__('First Name'),
'index' => 'firstname'
));
$this->addColumn('lastname', array(
'header' => Mage::helper('customer')->__('Last Name'),
'index' => 'lastname'
));*/
$this->addColumn('name', array(
'header' => Mage::helper('customer')->__('Name'),
'index' => 'name'
));
$this->addColumn('email', array(
'header' => Mage::helper('customer')->__('Email'),
'width' => '150',
'index' => 'email'
));
$this->addColumn('forum_username', array(
'header' => Mage::helper('customer')->__('Forum user name'),
'width' => '50px',
'index' => 'forum_username',
'type' => 'text',
));
$groups = Mage::getResourceModel('customer/group_collection')
->addFieldToFilter('customer_group_id', array('gt'=> 0))
->load()
->toOptionHash();
$this->addColumn('group', array(
'header' => Mage::helper('customer')->__('Group'),
'width' => '100',
'index' => 'group_id',
'type' => 'options',
'options' => $groups,
));
$this->addColumn('Telephone', array(
'header' => Mage::helper('customer')->__('Telephone'),
'width' => '100',
'index' => 'billing_telephone'
));
$this->addColumn('billing_postcode', array(
'header' => Mage::helper('customer')->__('ZIP'),
'width' => '90',
'index' => 'billing_postcode',
));
$this->addColumn('billing_country_id', array(
'header' => Mage::helper('customer')->__('Country'),
'width' => '100',
'type' => 'country',
'index' => 'billing_country_id',
));
$this->addColumn('billing_region', array(
'header' => Mage::helper('customer')->__('State/Province'),
'width' => '100',
'index' => 'billing_region',
));
$this->addColumn('customer_since', array(
'header' => Mage::helper('customer')->__('Customer Since'),
'type' => 'datetime',
'align' => 'center',
'index' => 'created_at',
'gmtoffset' => true
));
if (!Mage::app()->isSingleStoreMode()) {
$this->addColumn('website_id', array(
'header' => Mage::helper('customer')->__('Website'),
'align' => 'center',
'width' => '80px',
'type' => 'options',
'options' => Mage::getSingleton('adminhtml/system_store')->getWebsiteOptionHash(true),
'index' => 'website_id',
));
}
$this->addColumn('action',
array(
'header' => Mage::helper('customer')->__('Action'),
'width' => '100',
'type' => 'action',
'getter' => 'getId',
'actions' => array(
array(
'caption' => Mage::helper('customer')->__('Edit'),
'url' => array('base'=> '*/*/edit'),
'field' => 'id'
)
),
'filter' => false,
'sortable' => false,
'index' => 'stores',
'is_system' => true,
));
$this->addExportType('*/*/exportCsv', Mage::helper('customer')->__('CSV'));
$this->addExportType('*/*/exportXml', Mage::helper('customer')->__('Excel XML'));
return parent::_prepareColumns();
}
也是这样的安装程序---
<?php
$installer = $this;
$installer->startSetup();
/*Create Artist of the month */
$installer->addAttribute('customer', 'forum_username', array(
'input' => 'text',
'type' => 'varchar',
'label' => 'Forum Username',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
));
$attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "forum_username");
$used_in_forms=
array();
$used_in_forms[]="adminhtml_customer";
$attribute->setData("used_in_forms", $used_in_forms)
->setData("is_used_for_customer_segment", true)
->setData("is_system", 0)
->setData("is_user_defined", 1)
->setData("is_visible", 1)
->setData("sort_order", 100)
;
$attribute->save();
$installer->endSetup();