I created a custom attribute for the customer entity. I also have the adminhtml_customer
form, where the value can be changed and saved
.
The problem which I am facing is that, by default for all the customers the custom attribute is not present.
Only once I save it through the admin panel, that it appears as the data for that customer.
So, is there any way I can set a default value for the customer custom attribute which is not there itself by default.
The code which I used to make custom attribute :
<?php
$installer = $this;
$installer->startSetup();
$entityTypeId = (int)$installer->getEntityTypeId('customer');
$attributeSetId = (int)$installer->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = (int)$installer->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);
$installer->addAttribute( $entityTypeId, 'whatsapp_status', array(
'type' => 'int',
'label' => 'Whatsapp Status',
'input' => 'select',
'forms' => array('adminhtml_customer'),
'source' => 'dbaux_customer/source_whatsappOption',
'required' => false,
'visible' => 1,
'default' => 'Inactive',
'system' => 0,
'user_defined' => 1,
'nullable' => false,
));
$installer->addAttributeToGroup(
$entityTypeId,
$attributeSetId,
$attributeGroupId,
'whatsapp_status',
'999' //sort_order
);
$oAttribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'whatsapp_status');
$oAttribute->setData('used_in_forms', array('adminhtml_customer'));
$oAttribute->save();
$installer->endSetup();
My Source File :
<?php
class Dbaux_Customer_Model_Source_WhatsappOption 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")->__("New User"),
"value" => 0
),
array(
"label" => Mage::helper("eav")->__("Opted In"),
"value" => 1
),
array(
"label" => Mage::helper("eav")->__("Opted Out"),
"value" => 2
),
array(
"label" => Mage::helper("eav")->__("Not Registered"),
"value" => 3
),
);
}
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;
}
}