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:
- 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. - 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();