Back home
Mark Shust

Written by Mark Shust, a Certified Magento Developer, Architect & Teacher in Cleveland, Ohio.
Follow me @markshust

Create a customer address attribute in Magento with a custom installer script

October 21, 2015   ·   2 min read  ·   Edit on GitHub

Recently, I needed to create a custom customer address attribute, and needed a real simple way to do it. There are a lot of bloated examples online, but this is actually really easy.

First, create your module definition file. Note that we’re putting our module in the local code pool because it’s specific to just our one store.

app/etc/modules/Foo_Bar.xml
<?xml version="1.0"?>
<!--
 * @category    Foo
 * @package     Foo_Bar
-->
<config>
    <modules>
        <Foo_Bar>
            <active>true</active>
            <codePool>local</codePool>
        </Foo_Bar>
    </modules>
</config>

Then, we’ll create our config XML. Here, we are defining version 0.1.0, and simply setting up our resource setup definition.

app/code/local/Foo/Bar/etc/config.xml
<?xml version="1.0"?>
<!--
 * @category    Foo
 * @package     Foo_Bar
-->
<config>
    <modules>
        <Foo_Bar>
            <version>0.1.0</version>
        </Foo_Bar>
    </modules>
    <global>
        <resources>
            <foo_bar_setup>
                <setup>
                    <module>Foo_Bar</module>
                    <class>Mage_Eav_Model_Entity_Setup</class>
                </setup>
            </foo_bar_setup>
        </resources>
    </global>
</config>

Note how we don’t need to define a connection, or even create a setup definition for our module. All we are doing here is saying to create a new setup definition for our FooBar module, and use the MageEavModelEntity_Setup class, which will be used to run the installer script.

Finally, we’ll create our installer script in PHP. The location and naming of this file is important, as Magento does all of the lookups and routing in the backend to accomplish this. The customer address attribute we are creating is called baz. Note that we can only create attributes based off of MageCustomerModelEntitySetup with this method. If you want to create many attributes from different setup files, you’ll either need to create multiple models, or define your our setup resource model.

app/code/local/Foo/Bar/sql/foo_bar_setup/mysql4-install-0.1.0.php
<?php
/**
 * @category    Foo
 * @package     Foo_Bar
 */
 
/* @var $installer Mage_Eav_Model_Entity_Setup */
$installer = $this;
 
$installer->startSetup();
 
$this->addAttribute('customer_address', 'baz', array(
    'label'             => 'Baz',
    'type'              => 'varchar',
    'input'             => 'text',
    'position'          => 140,
    'visible'           => true,
    'required'          => false,
    'is_user_defined'   => true,
));
 
$installer->endSetup();

And that’s all she wrote! You’ll now be able to call your customer address attribute wherever you wish.

Share On Twitter
Let others know about this article

Learning Magento?

I'll send out tidbits (not more than once a week) explaining Magento 2 concepts to beginners.

    I won't send you spam. Unsubscribe at any time.