Adding attributes to customer entity

my current goal is to add a new customer attribute (with int type) which should appear as select with predefined options (loaded from a model with entries editable in backend, which is done). I'm struggling with proper use of $installer->addAttribute() method, especially specifying correct source option. Other problem is the new attribute isn't saved to eav_entity_attribute table

I'm on Magento CE 1.5.1.0


Solution 1:

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');
$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();

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

Solution 2:

@Jonathan Day's answer is great and helped me tremendously. However - as long as you've set your setup class to Mage_Customer_Model_Entity_Setup, then Magento can do all of that work for you:

<!-- 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>

And here is the mysql4-install-X.X.X.php file:

<?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();

The adminhtml_only above will handle all of the used_in_forms logic for you. Also, defining group will take care of assigning it to the attribute group.