Blog‎ > ‎

Extending Magento - Customer Attributes

posted Jun 9, 2012, 12:01 AM by Pasi Orovuo   [ updated Oct 3, 2012, 12:35 AM ]

It is very common to have to integrate Magento with a backend system, such as an ERP. Usually such backend system has and requires it's own unique identifier for customer ids etc. Magento does unfortunately not have the possibility to employ such id in customer attributes in order to link a customer in Magento database to a customer in ERP system. Below I'll describe the steps required to augment Magento's customer model with an additional field for backend/erp system customer identification.

Like all 3rd party Magento modules, the should reside in ${MAGENTO_DIR}/app/code/local folder. For starters, create the basic Magento customer module directory structure:

por:/var/www/magento/app/code/local$ tree RandomTargets/
   .
   |-RandomTargets
   |---Customer
   |-----etc

RandomTargets here defines the company name, Customer the module name (we're extending the customer module) and finally etc is the module configuration file. All modules need a configuration called config.xml in etc folder - in it's simplest form it's like this:

<?xml version="1.0" ?>
<config>
    <modules>
        <RandomTargets_Customer>
            <version>1.0.0</version>
        </RandomTargets_Customer>
    </modules>
</config>

And at this point it's best to make sure Magento can find the module and is able to activate it. This is done with an XML file in ${MAGENTO_DIR}/app/etc/modules (please note that it's outside the module directory). Here's RandomTargets_Customer.xml:

<?xml version="1.0" ?>
<config>
    <modules>
        <RandomTargets_Customer>
            <active>true</active>
            <codePool>local</codePool>
            
            <depends>
                <Mage_Customer />
            </depends>
        </RandomTargets_Customer>
    </modules>
</config>

At this point, if everything went right, after flushing all Magento caches, it is able to find and activate the module (see System / ConfigurationAdvanced / Disable modules output):

And it's there!

The module doesn't do much as it is but at least at this point it's sure that the base works.

Magento uses the concept of setup resources for managing changes in the model/database (more information here). First let's add required setup information in to the module configuration config.xml (added lines highlighted).

<?xml version="1.0" ?>
<config>
    <modules>
        <RandomTargets_Customer>
            <version>1.0.0</version>
        </RandomTargets_Customer>
    </modules>

    <global>
        <resources>
            <rt_customer_setup>
                <setup>
                    <module>RandomTargets_Customer</module>
                </setup>
            </rt_customer_setup>
            <connection>
                <use>core_setup</use>
            </connection>
        </resources>
    </global>
</config>

After changing the configuration a setup script must be made as well. Otherwise Magento will fail on next flush of cache.

Setup scripts reside in sql/${resourcename}_setup (sql/rt_customer_setup) in this case, so the final module directory structure looks like this:

por:/var/www/magento/app/code/local$ tree RandomTargets/
   .
   |-RandomTargets
   |---Customer
   |-----etc
   |-----sql
   |-------rt_customer_setup

A finally create the actual setup script mysql4-install-1.0.0.php in rt_customer_setup folder:

<?php

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup( 'core_setup' );

$installer->startSetup();

$setup->addAttribute( 'customer', 'rt_backend_customer_id', array(
        'group'         => 'Default',                           // Attribute group name
        'input'         => 'text',                              // UI input field type
        'type'          => 'varchar',                           // Database type
        'label'         => 'RT Backend Customer Id',            // UI label for field
        'visible'       => 1,
        'required'      => 1,
        'user_defined'  => 1,
    )
);

Mage::getSingleton( 'eav/config' )
    ->getAttribute( 'customer', 'rt_backend_customer_id' )
    ->setData( 'used_in_forms', array( 'adminhtml_customer' ) ) 
    ->save();

$installer->endSetup();

?>

Flush cache and take a look at customer management and the field should be visible. Please note that with this configuration the field is only available in the admin backend.

And here's a small Python script to test the api:

from xmlrpclib import ServerProxy
from pprint import pprint

proxy = ServerProxy( "http://127.0.0.1/magento/index.php/api/xmlrpc",
                     allow_none = True )
session = proxy.login( "apiUser", "apiKey" )

pprint( proxy.call( session, 'customer.list' ) )

And the result is...

por:~$ python customertest.py 
[{'created_at': '2011-06-20 23:02:06',
  'created_in': 'Admin',
  'customer_id': '1',
  'email': 'test.customer@randomtargets.net',
  'firstname': 'Test',
  'group_id': '1',
  'increment_id': '',
  'lastname': 'Customer',
  'middlename': '',
  'password_hash': 'SECRET!',
  'prefix': '',
  'rt_backend_customer_id': '6667',
  'store_id': '0',
  'suffix': '',
  'taxvat': '',
  'updated_at': '2011-06-20 23:02:06',
  'website_id': '0'}]

More to come...


Comments