Magento 1.9 – Set Custom Dropdown Attribute for Customer

ce-1.9.2.4customer-attributedropdown-attributemagento-1.9

I have create a custom attribute (dropdown) for customer as follow:

$installer = $this;
$installer->startSetup();
$entityTypeId     = (int)$installer->getEntityTypeId('customer');
$attributeSetId   = (int)$installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = (int)$installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$installer->addAttribute( $entityTypeId, 'customer_status', array(
    'type'               => 'int',
    'label'              => 'Customer Status',
    'input'              => 'select',
    'forms'              => array('adminhtml_customer'),
    'source'             => 'eav/entity_attribute_source_table',
    'required'           => false,
    'visible'            => 1,
    'position'           => 110,
    'option'             => array('values' => array('Inactive', 'Candidate', 'Certified')),
    'default'            => 'Inactive',
));

$installer->addAttributeToGroup($entityTypeId, $attributeSetId, $attributeGroupId, 'customer_status', 100);

$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'customer_status');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();

$installer->endSetup();

Attribute created successfully but:

  1. When I create a new customer, the default value is set to none, not
    inactive as in my case. How can I make Inactive default.
  2. How can I change the value of this attribute programmatically?

Q#2 Answer:
I was trying to change the attribute value by option text. Digging more into it shows that dropdown have some int value for option like below.

<select id="_accountcustomer_status" class=" select" name="account[customer_status]">
<option value=""></option>
<option selected="selected" value="241">Inactive</option>
<option value="242">Candidate</option>
<option value="243">Certified</option>
</select>

So Now using the following code, I'm able to get the value for required option and save it.

$attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter('customer_status')->getFirstItem();
$attributeOptions = $attributeInfo->getSource()->getAllOptions(false);
foreach($attributeOptions as $attr){
    if($attr['label'] == 'Candidate'){
        $statusValue = $attr['value'];
        break;
    }
}
        $customer->setCustomerStatus($statusValue);
        $customer->save();

Best Answer

You did not create Customer attribute properly.

As you have create a dropdown Customer attribute so you need to define source model for this attribute.

 "source"   => "[module_Model_Prefix]/source_option",

basically at this source class you have define option for that attribute

Also need to remove 'option' => array('values' => array('Inactive', 'Candidate', 'Certified')),

Source class

<?php
class [ModuleNameSpace]_[ModuleName]_Model_Source_Option extends Mage_Eav_Model_Entity_Attribute_Source_Abstract
{
    /**
     * Retrieve all options array
     *
     * @return array
     */
    public function getAllOptions()
    {
        if (is_null($this->_options)) {
            $this->_options = array(

                array(
                    "label" => Mage::helper("eav")->__("Inactive"),
                    "value" =>  1
                ),

                array(
                    "label" => Mage::helper("eav")->__("Candidate"),
                    "value" =>  2
                ),

            );
        }
        return $this->_options;
    }

    /**
     * Retrieve option array
     *
     * @return array
     */
    public function getOptionArray()
    {
        $_options = array();
        foreach ($this->getAllOptions() as $option) {
            $_options[$option["value"]] = $option["label"];
        }
        return $_options;
    }

    /**
     * Get a text for option value
     *
     * @param string|integer $value
     * @return string
     */
    public function getOptionText($value)
    {
        $options = $this->getAllOptions();
        foreach ($options as $option) {
            if ($option["value"] == $value) {
                return $option["label"];
            }
        }
        return false;
    }
Related Topic