0

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;
    }

}
0

I notice a typo in your installation script. You were trying to save a string, option label (varchar) as a default value for the customer entity attribute (whatsapp_status) of type integer, instead of the option value.

You should save 0 or 1. In your case, if you want to set Inactive as the default value for the given attribute, you should set the default value as 0. Cross check your source class file dbaux_customer/source_whatsappOption to confirm the respective option values for the options.

So it should be like this, which will set the inactive status as default for all the customers.

$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'            => 0,
    'system'             => 0,
    'user_defined'       => 1,
    'nullable'           => false,
));
| improve this answer | |
  • Thanks for the answer. Even when I did change the default to 0, it behaves the same way, with no attribute created for each customer until we save them specifically. I have added my source file code for reference. Please do tell where I am going wrong if possible. – arqam Jan 7 at 6:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.