Adding custom attribute to Customer



We need a simple way to add an attribute to a customer record that is not editable by the customer or admin, only programatically. Essentially, we have an ExpressionEngine site coupled with Magento.

We authenticate via webservice and would like to store some JSON that we get back from the authentication into the customer's record and update it each time they login.

We will also want the data modified if they change information in the checkout process, such as shipping address. We will then send the data back to our webservice as we currently do that with each order.

Is this difficult to do as we are now storing some JSON on each product using a custom attribute with MageWorx's Custom Options extension?

I used the Online Module Creator here but am not sure how to modify or retrieve the value once the module is installed.

Where could I learn how to write an extension to do this?


Posted 2014-01-30T21:17:44.740

Reputation: 191

How to do if I want to save this attribute value into 'customer_entity' database table? @Marius – Kazim Noorani – 2016-12-08T07:31:11.523

1@KazimNoorani If you want to save the value directly in the customer_entity table you need to add the column to the table and in the script that adds the attribute (see my answer below) replace the type from varchar to static. – Marius – 2016-12-08T07:36:00.713

@Marius I already added column in customer_entity table. And my attribute is of type 'select'. I want to save my attribute value directly at this 'custom_column' in customer_entity table. How to do that? – Kazim Noorani – 2016-12-08T07:52:53.427

1Even if you want to save the data in the main table you still need an attribute with the type static. – Marius – 2016-12-08T07:54:48.770

X-Ref: Adding attributes to customer entity (May 2011)

– hakre – 2015-05-19T08:41:20.877




$installer = $this;


$setup = new Mage_Eav_Model_Entity_Setup('core_setup');

$entityTypeId     = $setup->getEntityTypeId('customer');
$attributeSetId   = $setup->getDefaultAttributeSetId($entityTypeId);
$attributeGroupId = $setup->getDefaultAttributeGroupId($entityTypeId, $attributeSetId);

$setup->addAttribute("customer", "customattribute",  array(
    "type"     => "varchar",
    "backend"  => "",
    "label"    => "Custom Attribute",
    "input"    => "text",
    "source"   => "",
    "visible"  => true,
    "required" => false,
    "default" => "",
    "frontend" => "",
    "unique"     => false,
    "note"       => "Custom Attribute"


        $attribute   = Mage::getSingleton("eav/config")->getAttribute("customer", "customattribute");

    '999'  //sort_order


        $attribute->setData("used_in_forms", $used_in_forms)
                ->setData("is_used_for_customer_segment", true)
                ->setData("is_system", 0)
                ->setData("is_user_defined", 1)
                ->setData("is_visible", 1)
                ->setData("sort_order", 100)



 <?xml version="1.0"?>




  <?xml version="1.0"?>

Then to retrieve or edit you use:

$customer = Mage::getModel('customer/customer')->load($custid);

You will have to create event observers for the login event, answered here: How can i get customer data from observer after successful login?

and also likely observers for the customer_save_after in case they alter their address in account mgmt, and one for the quote, which could be in different places depending on what you are going for.


Posted 2014-01-30T21:17:44.740

Reputation: 1 196

Hi, i got this error message "Fatal error: Call to undefined method Mage_Core_Model_Resource_Setup::addAttribute() " i am using Magento 1.9.0.x – Josua Marcel Chrisano – 2016-03-12T02:54:37.470

What is customer_band_sku? – MB34 – 2014-01-30T22:46:59.737

Sorry, that was one I created leftover. – willboudle – 2014-01-31T02:26:16.557

So how would setCustomAttribute() work to set the data? – MB34 – 2014-01-31T15:33:59.517

Do you have an example of how to SET the data when the user logs in? – MB34 – 2014-02-06T22:52:17.083

Yes, I mention it above. Use Phil Winkles answer in the link to observe a customer logging in. In that observer you would use the above snippet: $customer->setCustomattribute($yourjson); – willboudle – 2014-02-07T13:16:06.250

Working good..could you also tell how to show that attribute in admin panel+ customer grid – aravind – 2017-11-14T09:59:34.123

By having &lt;class&gt;Mage_Customer_Model_Entity_Setup&lt;/class&gt; under global &gt; resources &gt; setup you make your module some what specific to setting up customer attributes. It may be better to not provide the &lt;class&gt; tag here, and instead change $installer = $this; to $installer = new Mage_Eav_Model_Entity_Setup('core_setup'); and ditch the $setup var. – Tyler V. – 2015-02-12T20:08:32.950


There is a lot of custom functionality you'll have to create yourself as custom module overriding classes, and hooking into events where you want data passed to your web service. As far as the attribute goes, when you create your custom module and define a setup resource for it in the module's config.xml like in the tutorial above, then in your install script you can do something like this:


$installer = $this;

$installer->startSetup ();

$setup = Mage::getModel ( 'customer/entity_setup' , 'core_setup' );

    //add budget
    $setup->addAttribute('customer', 'budget', array(
        'type' => 'decimal',
        'input' => 'text',
        'label' => 'Budget',
        'global' => 1,
        'visible' => 1,
        'required' => 0,
        'user_defined' => 0,
        'default' => '',
        'visible_on_front' => 1,
        'source' =>   NULL,
        'comment' => 'This is a budget'

$installer->endSetup ();

user_defined makes the attribute a system attribute if set to 0, which disables the ability to delete it from the admin.


Posted 2014-01-30T21:17:44.740

Reputation: 221