Zif*_*ius 37 php custom-attributes magento
我目前的目标是添加一个新的客户属性(带有int
类型),该属性应该显示为具有预定义选项的select(从具有可在后端编辑的条目的模型加载,这已完成).我正在努力正确使用$installer->addAttribute()
方法,特别是指定正确的源选项.其他问题是新属性未保存到eav_entity_attribute表
我在Magento CE 1.5.1.0上
Jon*_*Day 70
这是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();
Run Code Online (Sandbox Code Playgroud)
添加属性的不寻常步骤是,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);
Run Code Online (Sandbox Code Playgroud)
这里是一个步行通过使用自定义源为您的下拉
希望这有帮助,
JD
lee*_*eek 23
@ Jonathan Day的回答很棒,对我帮助很大.但是 - 只要您将setup
课程设置为Mage_Customer_Model_Entity_Setup
,那么Magento可以为您完成所有这些工作:
<!-- config.xml Example -->
<?xml version="1.0"?>
<config>
<global>
<resources>
<acme_module_setup>
<setup>
<module>Acme_Module</module>
<class>Mage_Customer_Model_Entity_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</acme_module_setup>
</resources>
</global>
</config>
Run Code Online (Sandbox Code Playgroud)
这是mysql4-install-X.X.X.php
文件:
<?php
$installer = $this;
/* @var $installer Mage_Customer_Model_Entity_Setup */
$installer->startSetup();
$installer->addAttribute(
'customer',
'acme_imported',
array(
'group' => 'Default',
'type' => 'int',
'label' => 'Imported into Acme',
'input' => 'select',
'source' => 'eav/entity_attribute_source_boolean',
'global' => Mage_Catalog_Model_Resource_Eav_Attribute::SCOPE_STORE,
'required' => 0,
'default' => 0,
'visible_on_front' => 1,
'used_for_price_rules' => 0,
'adminhtml_only' => 1,
)
);
$installer->endSetup();
Run Code Online (Sandbox Code Playgroud)
在adminhtml_only
上述将处理所有的used_in_forms
逻辑你.此外,定义group
将负责将其分配给属性组.
归档时间: |
|
查看次数: |
34539 次 |
最近记录: |