这是一个基本的int
属性和text
渲染器的代码:
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'your_attribute_code_here', array(
'input' => 'text',
'type' => 'int',
'label' => 'Some textual description',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
));
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$setup->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'your_attribute_code_here',
'999' //sort_order
);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'your_attribute_code_here');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();
$setup->endSetup();
为添加属性而采取的不寻常步骤是使用setData('used_in_forms')
, 这似乎对于客户属性是唯一的。如果没有它,该字段将不会被渲染,特别是在管理html中。你可以在customer_form_attribute
数据库表中查看此数组的有效选项。
至于使用具有预定义选项的select
,这就是你需要的:
$iAttributeId = $installer->getAttributeId($entityTypeId, 'your_attribute_code_here');
$aClasses = array('TV','DVD','Home Theatre','Air Conditioner','Stereo/Hifi','Game Console','Camcorder','VCR','Set Top Box','PVR');
$aOption = array();
$aOption['attribute_id'] = $iAttributeId;
for($iCount=0;$iCount<sizeof($aClasses);$iCount++){
$aOption['value']['option'.$iCount][0] = $aClasses[$iCount];
}
$setup->addAttributeOption($aOption);
这里有一个演示,介绍如何使用自定义数据源来创建下拉菜单。
希望对您有所帮助,
JD
INSERT INTO customer_form_attribute (form_code,attribute_id) VALUES('adminhtml_customer',159); UPDATE eav_entity_attribute SET sort_order = 0 WHERE (attribute_id='159')
解决方法: 在保存属性之前设置 sort_order 值:$oAttribute->setData('sort_order', $sortOrder);
- Fabian Schmengler安装错误:异常“Exception”,消息为“注意:在/var/www/vhosts/mage/app/code/core/Mage/Core/Model/Resource/Setup.php的第139行尝试获取非对象属性”/var/www/vhosts/mage/app/code/core/Mage/Core/functions.php:245”。 - Hashid Hameed