Lifted from:
This is the code for a basic int
attribute with text
renderer:
$installer = $this;
$installer->startSetup();
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$entityTypeId = $setup->getEntityTypeId('customer');
$attributeSetId = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$setup->addAttribute('customer', 'your_attribute_code_here', array(
'input' => 'text',
'type' => 'int',
'label' => 'Some textual description',
'visible' => 1,
'required' => 0,
'user_defined' => 1,
));
$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();
The unusual step for adding attributes is the setData('used_in_forms')
this seems to be unique to customer attributes. Without it, the field won't get rendered, certainly not in the adminhtml anyway. You can see the valid options for this array in the customer_form_attribute
database table.
In terms of using a select
with predefined options, this is what you need:
$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);
And here is a walk-through on using a custom source for your drop-down
Hope this helps,
JD
Best Answer
The customer fields are based on EAV which means an individual field can't be found in the template files but is stored in the database as an attribute.
In this case it's the
customer_eav_attribute
table. But first you'll need to know the attribute ID. For that check theeav_attribute
table, locate your attribute and use theattribute_id
value to locate the correct row in thecustomer_eav_attribute
table. Also setis_required
to 1 for this attribute in the table.If you take a look at the
customer_eav_attribute
table you'll see a couple of columns, for your question thevalidate_rules
column is the important one. It contains a serialized array with the types of validation.To make a field simply required adding
a:2:{s:15:"max_text_length";i:255;s:15:"min_text_length";i:1;}
should be enough.I haven't tested this so I'd suggest testing it on a development environment first.