Magento: Custom attributes for customer profiles
Posted on November 30th, 2010 in Magento, Web Development | 1 Comment »
If you’re looking to synchronize your customer, sales order and invoices between your back office system and Magento you’re going to need to store the back offices unique ID within Magento. Unlike products, adding custom attributes and attribute sets is not so straight forward. You can not just add it within the administration interface.
Task: Add field erp_customer_id to customer profile (same concept applies to sales orders and invoices)
Step 1: Let Magento know we are creating a new module and add TechAndHouse_Customer.xml in /app/etc/modules/ with the following:
<config> <modules> <TechAndHouse_Customer> <active>true</active> <codePool>local</codePool> </TechAndHouse_Customer> </modules> </config>
Step 2: Create a new module under /app/code/local/ with the following directory structure:
Step 3a: Copy the contents of /app/code/core/Mage/Customer/etc/config.xml into /app/code/local/TechAndHouse/Customer/etc/config.xml and change:
<modules> <Mage_Customer> <version>x.x.x</version> </Mage_Customer> </modules>
to
<modules> <TechAndHouse_Customer> <version>1.0.0</version> </TechAndHouse_Customer> </modules>
Step 3b: In this same file, config.xml add the variable erp_customer_id into the global fieldset under customer_account
<erp_customer_id><create>1</create><update>1</update></erp_customer_id>
Step 4a: Copy the getDefaultEntities method from /app/code/core/Mage/Customer/Model/Entity/Setup.php file to /app/code/local/TechAndHouse/Model/Entity/Setup.php and put it in the following class:
class TechAndHouse_Customer_Model_Entity_Setup extends Mage_Customer_Model_Entity_Setup {
Step 4b: Add the following attribute in the array right before website_id
'erp_customer_id' => array( 'label' => 'ERP Customer ID', 'visible' => true, 'required' => true, ),
Step 5: Add the following in any theme file and visit the page, like
$setup = new TechAndHouse_Customer_Model_Entity_Setup('core_setup'); $setup->addAttribute('customer', 'erp_customer_id', array( 'label' => 'ERP Customer ID', 'type' => 'varchar', 'input' => 'text', 'visible' => true, 'required' => true, 'position' => 1, ));
Step 6: Check if the variable got created in the eav_attribute table.
Step 7: Did it work? At this point you can run a quick test and see if you can write and read from your new attribute. Never modify core files, and do not do anything on a production machine. The following was done on a development box.
Add the following to /design/adminhtml/default/default/template/customer/tab/view.phtml:
$this->getCustomer()->setErpCustomerId("1234"); print_r($this->getCustomer()->getErpCustomerId());
Visit a customer in the admin section and you will see the following:
Credit
Original Article: Fontis

