Magento : Adding Custom Attribute to your Customers!

June 1, 2011 § 13 Comments

There are couple resources you need to go through,

  1. Magento Wiki : Custom Account/Registration Fields (2010-JUNE-25)
  2. Fontis : Know More About Your Customers – Adding Custom Signup Attributes ( 2009-JUN-15)
  3. Stackover : Can no longer add registration fields in Magento 1.4.2.0 (2010- DEC-28)

but the resource is so scattered, so I decided to gather the info here.

First of all, following people shall take the credit : Chris Woodard, AlexSz, phlux0r, bolasevich, Denis Margetic , Brian Wozeniak

So let’s get started, first of all,  backup your database first. Secondly, view the Magento Wiki : Custom Account/Registration Fields – the original post to get the first impression. I would recommend the Fontis : Know More About Your Customers – Adding Custom Signup Attributes as it suggested a non-core modification method to tackle the issue, and a clearer article structure as well.

Please following Fontis : Know More About Your Customers – Adding Custom Signup Attributes until step 4 finished.

For Step 5 we would modify the code a bit to make it magento 1.5 compatible.

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'flavour', array(
	'label'		=> 'Ice Cream Flavour',
	'type'		=> 'varchar',
	'input'		=> 'text',
	'visible'	=> true,
	'required'	=> true,
	'position'	=> 1,
	));
/* From Stackover http://stackoverflow.com/questions/4549112/can-no-longer-add-registration-fields-in-magento-1-4-2-0*/
$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('customer', 'flavour');
$attribute->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit'));  //enable all action
//$attribute->setData('used_in_forms', array('adminhtml_customer'));  //to make the attribute can be created in backend only
//$attribute->setData('used_in_forms', array('customer_account_create')); //to make the attribute can be created in registration only
//$attribute->setData('used_in_forms', array('customer_account_edit')); // to make the attribute can be edited in the frontend only

$attribute->save();

Not sure if we can call setData for the attribute in a single go, but it illustrates the idea

After that follow the rest of the steps is fine.

Database Hack

If you already went through the articles before coming to this one. You can always use the database style to sort things out.

From Brian Wozeniak :

Magento 1.5 got a new table called  customer_form_attribute that other articles didnt cover.

INSERT INTO `database`.`customer_form_attribute` (`form_code`, `attribute_id`) VALUES ('adminhtml_customer', 'YOUR_ATTRIBUTE_ID');
INSERT INTO `database`.`customer_form_attribute` (`form_code`, `attribute_id`) VALUES ('customer_account_create', 'YOUR_ATTRIBUTE_ID');
INSERT INTO `database`.`customer_form_attribute` (`form_code`, `attribute_id`) VALUES ('customer_account_edit', 'YOUR_ATTRIBUTE_ID');

It should save some times 😛

Advertisements

Tagged: , , ,

§ 13 Responses to Magento : Adding Custom Attribute to your Customers!

  • This article very helpful.
    Thanks so much.

  • Mullanaphy! says:

    Awesome, the INSERTS from Brian Wozeniak were exactly what I was looking for. Thanks for helping my brain from not exploding trying to find out how to get the fields to show up via form in 1.5.

    Thanks!

  • Cesar says:

    Thanks a lot!

    As the documentation is sparse, we depent hugely on guys like you and the one on Fontis on posting such information. 🙂

    I’d like to complete, on edit.phtml someone would use getCustomer() instead of getFormData(). I was having issues on this till I figure whats wrong.

    See ya.

  • Alin says:

    will attributes created with this method show up on invoices?

    • nickwanhere says:

      Hi Alin,

      I dont think it will automatically include in the invoices, but you should be able to call the attribute from the invoice controllers. Is it the Pdf invoice you are talking about?

      • Alin says:

        no, the invoice that gets sent to the customer. also, on step 5, can i setup multiple attributes to the database like this?

        $setup->addAttribute(‘customer’, ‘flavour’, array(
        ‘label’ => ‘Ice Cream Flavour’,
        ‘type’ => ‘varchar’,
        ‘input’ => ‘text’,
        ‘visible’ => true,
        ‘required’ => true,
        ‘position’ => 1,
        ));

        $setup->addAttribute(‘customer’, ‘jobtitle’, array(
        ‘label’ => ‘Job Title’,
        ‘type’ => ‘varchar’,
        ‘input’ => ‘text’,
        ‘visible’ => true,
        ‘required’ => true,
        ‘position’ => 1,
        ));
        and then call them both here like this?

        $attribute = $eavConfig->getAttribute(‘customer’, ‘flavour’, ‘jobtitle’);

        how can i do this?

      • nickwanhere says:

        The code you posted is just helping you to add the attribute to the existing database only.
        I am sure you can do it all at one time, but I would add them separately.

        $attribute = $eavConfig->getAttribute('customer', 'flavour', 'jobtitle');
        

        I dun think you can call two attribute at one time. Do it one by one.

        Also I think by calling , you mean calling the attribute value that associated with the customer, right?

        $customer=Mage::getModel('customer/customer')->load(YOUR_CUSTOMER_ID);
        $customer->getFlavour();
        $customer->getJobtitle();
        

        This is only a wild guess, feel free to try it out, but backup your data first! Good Luck

  • Alin says:

    can you help me with an example on how to call them one by one?

    Thank you!

    Alin.

  • Reeno says:

    And how to add dropdown attribute with some options?

  • aman says:

    good post. but i prefer use magento extension for such deals.

    For customer attributes i use extension by amasty.

    I’m sure that it is best solution for customer attributes.

    • nickwanhere says:

      I am sure Amasty would do a better job, but sometimes you know we want to prove got the ability , in order word, we do not have the budget to pay :[

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

What’s this?

You are currently reading Magento : Adding Custom Attribute to your Customers! at Nick Wan L.K..

meta

%d bloggers like this: