Create new customer attribute magento
Posted by navaneeth on Oct 5, 2014 in Magento | No comments yet

Magento itself have lot of attributes inbuilt for customers, sometimes we need to add a new attribute to customer.
So here is the code to create customer attribute in magento
In the below example we are adding “Country of Residence” as a new attribute to customer
First we need to load Mage.php
require_once 'app/Mage.php'; Mage::init(); $installer = new Mage_Customer_Model_Entity_Setup('core_setup'); $installer->startSetup(); $installer->addAttribute("customer", "country_residence", array( "type" => "varchar", "backend" => "", "label" => "Country of Residence", "input" => "text", "source" => "", "visible" => true, "required" => false, "default" => "", "frontend" => "", "unique" => false, "note" => "" )); $attribute = Mage::getSingleton("eav/config")->getAttribute("customer", "country_residence"); $used_in_forms=array(); $used_in_forms[]="adminhtml_customer"; $used_in_forms[]="checkout_register"; $used_in_forms[]="customer_account_create"; $used_in_forms[]="customer_account_edit"; $used_in_forms[]="adminhtml_checkout"; $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) ; $attribute->save();
We can see an secons section of the code in which it is used for where to display the attributes
Hope this code snippet helps you to create customer attribute in magento
Leave a Reply