29

I like to add new field in customer registration and admin customer creation form.

The field name is license number. I searched so many links related to my requirement but those do not work in Magento (1.9). Even I found the extension related to this:

http://www.magentocommerce.com/magento-connect/custome-account-profile-13594.html

When I installed the above extension using key, it shows an error. Can you please give some idea related to my requirement?

3
  • Also provide what error you are getting in above extension...please send as much details as you can.. Commented May 5, 2015 at 12:42
  • This has been previously answered here: magento.stackexchange.com/questions/14163/…
    – tecjam
    Commented May 5, 2015 at 12:49
  • Any help to add license number at checkout page billing and shipping section?
    – Gem
    Commented Mar 10, 2020 at 11:51

1 Answer 1

56

You need to create a new extension to make it clean.
Let's call the extension StackExchange_Customer.
You will need the following files:

app/etc/modules/StackExchange_Customer.xml - the declaration file

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

app/code/local/StackExchange/Customer/etc/config.xml - the configuration file

<?xml version="1.0"?>
<config>
    <modules>
        <StackExchange_Customer>
            <version>1.0.0</version>
        </StackExchange_Customer>
    </modules>
    <global>
        <helpers>
            <stackexchange_customer>
                <class>StackExchange_Customer_Helper</class>
            </stackexchange_customer>
        </helpers>
        <resources>
            <stackexchange_customer_setup>
                <setup>
                    <module>StackExchange_Customer</module>
                    <class>Mage_Customer_Model_Resource_Setup</class>
                </setup>
            </stackexchange_customer_setup>
        </resources>
    </global>
    <frontend>
        <layout>
            <updates>
                <stackexchange_customer>
                    <file>stackexchange_customer.xml</file>
                </stackexchange_customer>
            </updates>
        </layout>
        <translate>
            <modules>
                <StackExchange_Customer>
                    <files>
                        <default>StackExchange_Customer.csv</default>
                    </files>
                </StackExchange_Customer>
            </modules>
        </translate>
    </frontend>
</config>

app/code/local/StackExchange/Customer/sql/stackexchange_customer_setup/install-1.0.0.php - the install file. Will add the new attribute.

<?php
$this->addAttribute('customer', 'license_number', array(
    'type'      => 'varchar',
    'label'     => 'License Number',
    'input'     => 'text',
    'position'  => 120,
    'required'  => false,//or true
    'is_system' => 0,
));
$attribute = Mage::getSingleton('eav/config')->getAttribute('customer', 'license_number');
$attribute->setData('used_in_forms', array(
    'adminhtml_customer',
    'checkout_register',
    'customer_account_create',
    'customer_account_edit',
));
$attribute->setData('is_user_defined', 0);
$attribute->save();

app/code/local/StackExchange/Customer/Helper/Data.php - the module main helper

<?php
class StackExchange_Customer_Helper_Data extends Mage_Core_Helper_Abstract
{

}

This will add your attribute for the customer.
It should work nicely on the backend.
Unfortunately you have to edit the frontend templates manually now because Magento does not have any event or empty block where you can put your fields.
For this you need the following.

app/design/frontend/base/default/layout/stackexchange_customer.xml

<?xml version="1.0"?>
<layout>
    <customer_account_edit>
        <reference name="customer_edit">
            <action method="setTemplate">
                <template>stackexchange_customer/form/edit.phtml</template>
            </action>
        </reference>
    </customer_account_edit>
    <customer_account_create>
        <reference name="customer_form_register">
            <action method="setTemplate">
                <template>stackexchange_customer/register.phtml</template>
            </action>
        </reference>
    </customer_account_create>
</layout>

And now the templates.

app/design/frontend/base/default/template/stackexchange_customer/register.phtml - the registration template.
For this one make a clone of the /app/design/frontend/{package}/{theme}/template/persistent/customer/form/register.phtml and just insert this somewhere inside the form. I don't need to post the full file here. Arrange it as you please

<li>
    <label for="license_number"><?php echo $this->__('License Number') ?></label>
    <div class="input-box">
        <input type="text" name="license_number" id="license_number" value="<?php echo $this->escapeHtml($this->getFormData()->getLicenseNumber()) ?>" title="<?php echo $this->__('License Number') ?>" class="input-text" />
    </div>
</li>

/app/design/frontend/base/default/template/stackexchange_customer/form/edit.phtml For this one clone /app/design/frontend/{package}/{theme}/template/customer/form/edit.phtml and insert somewhere inside the form this:

<li>
    <label for="license_number"><?php echo $this->__('License Number') ?></label>
    <div class="input-box">
        <input type="text" name="license_number" id="license_number" value="<?php echo $this->htmlEscape($this->getCustomer()->getLicenseNumber()) ?>" title="<?php echo $this->__('License Number') ?>" class="input-text" />
    </div>
</li>

You can also create the translation file. Is not mandatory but it's nice to have

app/locale/en_US/StackExchange_Customer.csv

"License Number","License Number"

Clear the cache and you should be set.

18
  • 1
    @senthil. You are right. Sorry about that. I pasted the wrong install script. I updated the answer with the correct app/code/local/StackExchange/Customer/sql/stackexchange_customer_setup/install-1.0.0.php file. Since you already installed the module, the new install script won't run again. To make it run again you need to delete from the table eav_attribute the attribute with the code license_number and from the table core_resource the record with the code StackExchange_Customer. Clear the cache and refresh the page. The install script should run again.
    – Marius
    Commented May 6, 2015 at 6:16
  • 3
    @senthil If you plan to add other fields you should clone install-1.0.0 to upgrade-1.0.0-1.0.1 and do your modifications in the cloned file. Then change the version tag in config.xml to 1.0.1. See how upgrade scripts work here: magento.stackexchange.com/q/26313/146
    – Marius
    Commented May 6, 2015 at 6:49
  • 1
    Hi sir ,you didnt tell us How to set multiple attributes ? I tried for multiple attrs by modifying your code. Still not working . Please explain Commented Jul 17, 2015 at 9:44
  • 1
    @Haris, for uploads, you most probably have to do something extra, but I don't know what.
    – Marius
    Commented Sep 7, 2016 at 11:04
  • 1
    @Marius I just want area field from billing and shipping section at the checkout page, help me thanks.
    – Gem
    Commented Mar 10, 2020 at 11:46

Not the answer you're looking for? Browse other questions tagged or ask your own question.