gpt4 book ai didi

php - 向客户实体添加属性

转载 作者:IT王子 更新时间:2023-10-29 00:58:45 24 4
gpt4 key购买 nike

我当前的目标是添加一个新的客户属性(具有 int 类型),它应该显示为带有预定义选项的选择(从具有可在后端编辑的条目的模型加载,已完成)。我正在努力正确使用 $installer->addAttribute() 方法,尤其是指定正确的源选项。另一个问题是新属性没有保存到 eav_entity_attribute 表

我在使用 Magento CE 1.5.1.0

最佳答案

这是带有 text 渲染器的基本 int 属性的代码:

$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') 这似乎是客户属性所独有的。没有它,该字段将不会被渲染,当然也不会在 adminhtml 中渲染。您可以在 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);

这是一个 walk-through关于为下拉菜单使用自定义来源

希望对您有所帮助,
京东

关于php - 向客户实体添加属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5961290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com