PHP Magento Tips

Add New Field to Customer Registration Form in Magento

Rate this posting:
{[['']]}

To add new field to Customer Registration form, you have to create new module.

Just follow below steps to make it happen !!

Step 1: Create Module initialization file. Create app/etc/modules/Mycompany_Customer.xml file and add below code in it.

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

Step 2: Create config.xml file. Create app/code/local/Mycompany/Customer/etc/config.xml file and add below code in it.

<?xml version="1.0"?>
<config>
    <modules>
        <Mycompany_Customer>
            <version>1.0.0</version>
        </Mycompany_Customer>
    </modules>
    <global>
        <helpers>
            <mycompany_customer>
                <class>Mycompany_Customer_Helper</class>
            </mycompany_customer>
        </helpers>
        <resources>
            <mycompany_customer_setup>
                <setup>
                    <module>Mycompany_Customer</module>
                    <class>Mage_Customer_Model_Resource_Setup</class>
                </setup>
            </mycompany_customer_setup>
        </resources>
    </global>
    <frontend>
        <layout>
            <updates>
                <mycompany_customer>
                    <file>mycompany_customer.xml</file>
                </mycompany_customer>
            </updates>
        </layout>
    </frontend>
</config>

Step 3: Create install script to add new attribute. Create file app/code/local/Mycompany/Customer/sql/mycompany_customer_setup/install-1.0.0.php and add below code in it.

<?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();

Step 4: Create Helper File. Create file in app/code/local/Mycompany/Customer/Helper/Data.php and add below code in it.

<?php
class Mycompany_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 to follow below steps.

Step 5: Create frontend layout file. Create app/design/frontend/rwd/default/layout/mycompany_customer.xml file and add below code in it.

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

Step 6: Create template files.

a:> Create file in app/design/frontend/rwd/default/template/mycompany_customer/form/register.phtml

For registeration file make a clone of the app/design/frontend/rwd/default/template/persistent/customer/form/register.phtml file and add your new field here. I added new field just after Email Address.

<div class="account-create">
    <div class="page-title">
        <h1><?php echo $this->__('Create an Account') ?></h1>
    </div>
    <?php echo $this->getChildHtml('form_fields_before')?>
    <?php echo $this->getMessagesBlock()->toHtml() ?>
    <form action="<?php echo $this->getPostActionUrl() ?>" method="post" id="form-validate" class="scaffold-form" enctype="multipart/form-data">
        <div class="fieldset">
            <input type="hidden" name="success_url" value="<?php echo $this->getSuccessUrl() ?>" />
            <input type="hidden" name="error_url" value="<?php echo $this->getErrorUrl() ?>" />
            <input type="hidden" name="form_key" value="<?php echo Mage::getSingleton('core/session')->getFormKey() ?>" />
            <p class="form-instructions"><?php echo $this->__('Please enter the following information to create your account.') ?></p>
            <p class="required"><?php echo $this->__('* Required Fields') ?></p>
            <ul class="form-list">
                <li class="fields">
                    <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getFormData())->setForceUseCustomerAttributes(true)->toHtml() ?>
                </li>
                <li>
                    <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                    <div class="input-box">
                        <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="email" id="email_address" value="<?php echo $this->escapeHtml($this->getFormData()->getEmail()) ?>" title="<?php echo $this->quoteEscape($this->__('Email Address')) ?>" class="input-text validate-email required-entry" />
                    </div>
                </li>
                <!-- new field start here -->
                <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>
                <!-- new field Ends here -->
            <?php $_dob = $this->getLayout()->createBlock('customer/widget_dob') ?>
            <?php if ($_dob->isEnabled()): ?>
                <li><?php echo $_dob->setDate($this->getFormData()->getDob())->toHtml() ?></li>
            <?php endif ?>
            <?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat') ?>
            <?php if ($_taxvat->isEnabled()): ?>
                <li><?php echo $_taxvat->setTaxvat($this->getFormData()->getTaxvat())->toHtml() ?></li>
            <?php endif ?>
            <?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?>
            <?php if ($_gender->isEnabled()): ?>
                <li><?php echo $_gender->setGender($this->getFormData()->getGender())->toHtml() ?></li>
            <?php endif ?>

            <?php if($this->getShowAddressFields()): ?>
                <li class="hidden">
                    <input type="hidden" name="create_address" value="1" />
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="company"><?php echo $this->__('Company') ?></label>
                        <div class="input-box">
                            <input type="text" name="company" id="company" value="<?php echo $this->escapeHtml($this->getFormData()->getCompany()) ?>" title="<?php echo $this->quoteEscape($this->__('Company')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('company') ?>" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="telephone" class="required"><em>*</em><?php echo $this->__('Telephone') ?></label>
                        <div class="input-box">
                            <input type="tel" name="telephone" id="telephone" value="<?php echo $this->escapeHtml($this->getFormData()->getTelephone()) ?>" title="<?php echo $this->quoteEscape($this->__('Telephone')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('telephone') ?>" />
                        </div>
                    </div>
                </li>
            <?php $_streetValidationClass = $this->helper('customer/address')->getAttributeValidationClass('street'); ?>
                <li class="wide">
                    <label for="street_1" class="required"><em>*</em><?php echo $this->__('Street Address') ?></label>
                    <div class="input-box">
                        <input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet(0)) ?>" title="<?php echo $this->quoteEscape($this->__('Street Address')) ?>" id="street_1" class="input-text <?php echo $_streetValidationClass ?>" />
                    </div>
                </li>
            <?php $_streetValidationClass = trim(str_replace('required-entry', '', $_streetValidationClass)); ?>
            <?php for ($_i = 2, $_n = $this->helper('customer/address')->getStreetLines(); $_i <= $_n; $_i++): ?>
                <li class="wide">
                    <label for="billing:street<?php echo $_i ?>"><?php echo $this->__('Street Address %s', $_i) ?></label>
                    <div class="input-box">
                        <input type="text" name="street[]" value="<?php echo $this->escapeHtml($this->getFormData()->getStreet($_i - 1)) ?>" title="<?php echo $this->quoteEscape($this->__('Street Address %s', $_i)) ?>" id="street_<?php echo $_i ?>" class="input-text <?php echo $_streetValidationClass ?>" />
                    </div>
                </li>
            <?php endfor; ?>
                <li class="fields">
                    <div class="field">
                        <label for="city" class="required"><em>*</em><?php echo $this->__('City') ?></label>
                        <div class="input-box">
                            <input type="text" name="city" value="<?php echo $this->escapeHtml($this->getFormData()->getCity()) ?>" title="<?php echo $this->quoteEscape($this->__('City')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('city') ?>" id="city" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="region_id" class="required"><em>*</em><?php echo $this->__('State/Province') ?></label>
                        <div class="input-box">
                            <select id="region_id" name="region_id" title="<?php echo $this->quoteEscape($this->__('State/Province')) ?>" class="validate-select" style="display:none;">
                                <option value=""><?php echo $this->__('Please select region, state or province') ?></option>
                            </select>
                            <script type="text/javascript">
                            //<![CDATA[
                                $('region_id').setAttribute('defaultValue', "<?php echo $this->getFormData()->getRegionId() ?>");
                            //]]>
                            </script>
                            <input type="text" id="region" name="region" value="<?php echo $this->escapeHtml($this->getRegion()) ?>" title="<?php echo $this->quoteEscape($this->__('State/Province')) ?>" class="input-text <?php echo $this->helper('customer/address')->getAttributeValidationClass('region') ?>" style="display:none;" />
                        </div>
                    </div>
                </li>
                <li class="fields">
                    <div class="field">
                        <label for="zip" class="required"><em>*</em><?php echo $this->__('Zip/Postal Code') ?></label>
                        <div class="input-box">
                            <input type="text" name="postcode" value="<?php echo $this->escapeHtml($this->getFormData()->getPostcode()) ?>" title="<?php echo $this->quoteEscape($this->__('Zip/Postal Code')) ?>" id="zip" class="input-text validate-zip-international <?php echo $this->helper('customer/address')->getAttributeValidationClass('postcode') ?>" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="country" class="required"><em>*</em><?php echo $this->__('Country') ?></label>
                        <div class="input-box">
                            <?php echo $this->getCountryHtmlSelect() ?>
                        </div>
                    </div>
                </li>
                <li class="hidden">
                    <input type="hidden" name="default_billing" value="1" />
                    <input type="hidden" name="default_shipping" value="1" />
                </li>
            <?php endif; ?>
                <li class="fields">
                    <div class="field">
                        <label for="password" class="required"><em>*</em><?php echo $this->__('Password') ?></label>
                        <div class="input-box">
                            <input type="password" name="password" id="password" title="<?php echo $this->quoteEscape($this->__('Password')) ?>" class="input-text required-entry validate-password" />
                        </div>
                    </div>
                    <div class="field">
                        <label for="confirmation" class="required"><em>*</em><?php echo $this->__('Confirm Password') ?></label>
                        <div class="input-box">
                            <input type="password" name="confirmation" title="<?php echo $this->quoteEscape($this->__('Confirm Password')) ?>" id="confirmation" class="input-text required-entry validate-cpassword" />
                        </div>
                    </div>
                </li>
                <?php echo $this->getChildHtml('form.additional.info'); ?>                
                <?php if ($this->isNewsletterEnabled()): ?>
                    <li class="control">
                        <div class="input-box">
                            <input type="checkbox" name="is_subscribed" title="<?php echo $this->quoteEscape($this->__('Sign Up for Newsletter')) ?>" value="1" id="is_subscribed"<?php if($this->getFormData()->getIsSubscribed()): ?> checked="checked"<?php endif; ?> class="checkbox" />
                        </div>
                        <label for="is_subscribed"><?php echo $this->__('Sign Up for Newsletter') ?></label>
                    </li>
                <?php endif ?>
                <?php echo $this->getChildHtml('persistent.remember.me'); ?>
            </ul>
            <?php echo $this->getChildHtml('persistent.remember.me.tooltip'); ?>
        </div>
        <div class="buttons-set">
            <p class="back-link"><a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>" class="back-link"><small>&laquo; </small><?php echo $this->__('Back') ?></a></p>
            <button type="submit" title="<?php echo $this->quoteEscape($this->__('Register')) ?>" class="button"><span><span><?php echo $this->__('Register') ?></span></span></button>
        </div>
        <?php if (Mage::helper('checkout')->isContextCheckout()): ?>
            <input name="context" type="hidden" value="checkout" />
        <?php endif; ?>
    </form>
    <script type="text/javascript">
    //<![CDATA[
        var dataForm = new VarienForm('form-validate', true);
        <?php if($this->getShowAddressFields()): ?>
        new RegionUpdater('country', 'region', 'region_id', <?php echo $this->helper('directory')->getRegionJson() ?>, undefined, 'zip');
        <?php endif; ?>
    //]]>
    </script>
</div>

b:> Create file in app/design/frontend/rwd/default/template/mycompany_customer/form/edit.phtml

For registeration edit file make a clone of the app/design/frontend/rwd/default/template/customer/form/edit.phtml file and add your new field here. I added new field just after Email Address.

<div class="page-title">
    <h1><?php echo $this->__('Edit Account Information') ?></h1>
</div>
<?php echo $this->getMessagesBlock()->toHtml() ?>
<form action="<?php echo $this->getUrl('customer/account/editPost') ?>" method="post" autocomplete="off" id="form-validate" class="scaffold-form" enctype="multipart/form-data">
    <div class="fieldset">
        <?php echo $this->getBlockHtml('formkey')?>
        <h2 class="legend"><?php echo $this->__('Account Information') ?></h2>
        <p class="required"><?php echo $this->__('* Required Fields') ?></p>
        <ul class="form-list">
            <li class="fields">
                <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
            </li>
            <li>
                <label for="email" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label>
                <div class="input-box">
                    <input type="email" autocapitalize="off" autocorrect="off" spellcheck="false" name="email" id="email" value="<?php echo $this->escapeHtml($this->getCustomer()->getEmail()) ?>" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Email Address')) ?>" class="input-text required-entry validate-email" />
                </div>
            </li>
            <!-- new field start here -->
            <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>
   <!-- new field ends here -->
        <?php $_dob = $this->getLayout()->createBlock('customer/widget_dob') ?>
        <?php if ($_dob->isEnabled()): ?>
            <li><?php echo $_dob->setDate($this->getCustomer()->getDob())->toHtml() ?></li>
        <?php endif ?>
        <?php $_taxvat = $this->getLayout()->createBlock('customer/widget_taxvat') ?>
        <?php if ($_taxvat->isEnabled()): ?>
            <li><?php echo $_taxvat->setTaxvat($this->getCustomer()->getTaxvat())->toHtml() ?></li>
        <?php endif ?>
        <?php $_gender = $this->getLayout()->createBlock('customer/widget_gender') ?>
        <?php if ($_gender->isEnabled()): ?>
            <li><?php echo $_gender->setGender($this->getCustomer()->getGender())->toHtml() ?></li>
        <?php endif ?>
            <li class="control">
                <input type="checkbox" name="change_password" id="change_password" value="1" onclick="setPasswordForm(this.checked)" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Change Password')) ?>"<?php if($this->getCustomer()->getChangePassword()==1): ?> checked="checked"<?php endif; ?> class="checkbox" /><label for="change_password"><?php echo $this->__('Change Password') ?></label>
            </li>
        </ul>
    </div>
    <div class="fieldset" style="display:none;">
        <h2 class="legend"><?php echo $this->__('Change Password') ?></h2>
        <ul class="form-list">
            <li>
                <label for="current_password" class="required"><em>*</em><?php echo $this->__('Current Password') ?></label>
                <div class="input-box">
                    <!-- This is a dummy hidden field to trick firefox from auto filling the password -->
                    <input type="text" class="input-text no-display" name="dummy" id="dummy" />
                    <input type="password" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Current Password')) ?>" class="input-text" name="current_password" id="current_password" />
                </div>
            </li>
            <li class="fields">
                <div class="field">
                    <label for="password" class="required"><em>*</em><?php echo $this->__('New Password') ?></label>
                    <div class="input-box">
                        <input type="password" title="<?php echo Mage::helper('core')->quoteEscape($this->__('New Password')) ?>" class="input-text validate-password" name="password" id="password" />
                    </div>
                </div>
                <div class="field">
                    <label for="confirmation" class="required"><em>*</em><?php echo $this->__('Confirm New Password') ?></label>
                    <div class="input-box">
                        <input type="password" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Confirm New Password')) ?>" class="input-text validate-cpassword" name="confirmation" id="confirmation" />
                    </div>
                </div>
            </li>
        </ul>
    </div>
    <div class="buttons-set">
        <p class="back-link"><a href="<?php echo $this->escapeUrl($this->getBackUrl()) ?>"><small>&laquo; </small><?php echo $this->__('Back') ?></a></p>
        <button type="submit" title="<?php echo Mage::helper('core')->quoteEscape($this->__('Save')) ?>" class="button"><span><span><?php echo $this->__('Save') ?></span></span></button>
    </div>
</form>
<script type="text/javascript">
//<![CDATA[
    var dataForm = new VarienForm('form-validate', true);
    function setPasswordForm(arg){
        if(arg){
            $('current_password').up(3).show();
            $('current_password').addClassName('required-entry');
            $('password').addClassName('required-entry');
            $('confirmation').addClassName('required-entry');

        }else{
            $('current_password').up(3).hide();
            $('current_password').removeClassName('required-entry');
            $('password').removeClassName('required-entry');
            $('confirmation').removeClassName('required-entry');
        }
    }

    <?php if($this->getCustomer()->getChangePassword()): ?>
        setPasswordForm(true);
    <?php endif; ?>
//]]>
</script>

Now Clear the cache and check your registration form and Account information tab for both admin and front end.
Tag : Magento
0 Comments On "Add New Field to Customer Registration Form in Magento"

Back To Top