Add attribute to customer in Magento

21.Mar.2011

Simple question that developers in Magento sometimes ask is: “How can I add custom attribute to customers when using Magento”. The answer is simple – it is shown in the snippet below. But before you use it lets imagine what we want to do… Lets say that we want to give discount to users that payed by using preferred method, and that we want to be able to change that customer attribute in our Magento admin. So – we should run following code – once – anywhere in your Magento installation.

After you have run this script, your Magento customer will have new attribute in administration area. You will be able to access it from your code and allow administrators to change this attribute. This added customer attribute in your Magento admin will have label “Preferred payment method”, and variable name “client_payment_method”.

1
2
3
4
5
6
7
8
9
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'client_payment_method', array(
	'label'		=> 'Preferred payment method',
	'type'		=> 'text',
	'input'		=> 'text',
	'visible'	=> true,
	'required'	=> false,
	'position'	=> 1,
));

Once you ran this script – you can delete it from your code. Enjoy the functionality you have achieved with custom added attribute to Magento customer!