Adding custom registration field on magento
With this blog we will see how easily we can add any custom fields on magento registration page. We will add SSN# on the registration field
Note:
Althogh it is highly recommended to not touch the core files of magento because at the time of version updation magento updates the core files so if you made any changes in the core files then it will get override with new version. So as a standard practice always create a module if you want to override any feature or function of core magento. We will discuss this point later more precisiously.
In order to add custom fields on registration pages of magento you only need to touch 5 following files
i)app/design/frontend/base/default/template/customer/form/register.phtml
ii)app/code/core/mage/customer/etc/config.xml.
iii)app/code/core/mage/customer/model/Entity/Setup.php
iv)app/design/frontend/base/default/template/customer/form/edit.phtml
v)app/code/core/mage/customre/controllers/accountController.php
Step 1: On config.xml (We are going to add a new field there SSN No.)
find
<customer_account> <prefix><create>1</create><update>1</update><name>1</name></prefix>
and replace it with
<customer_account> <prefix><create>1</create><update>1</update><name>1</name></prefix> <ssn><create>1</create><update>1</update><name>1</name></ssn>
Step 2: On setup.php (Going to add one field in database by using this)
Find
'prefix' => array( 'label' => 'Prefix', 'required' => false, 'sort_order' => 37, )
and replace it with
'prefix' => array( 'label' => 'Prefix', 'required' => false, 'sort_order' => 37, ), 'ssn' => array( 'required' => true, 'label' => 'SSN', ),
Step 3: On register.phtml (HTML Based amendment going here)
Find
<li> <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address12') ?></label> <div class="input-box"> <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" /> </div> </li>
and replace it with
<li> <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address12') ?></label> <div class="input-box"> <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" /> </div> </li> <li> <label for="ssn" class="required"><em>*</em><?php echo $this->__('SSN #') ?></label> <div class="input-box"> <input type="text" name="ssn" id="ssn" value="<?php echo $this->htmlEscape($this->getFormData()->getSsn()) ?>" title="<?php echo $this->__('Ssn') ?>" class="input-text required-entry" /> </div> </li>
Step 4: To show data on edit page just open edit.phtml and find (For Edit registration page)
<?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
And replace it with
<?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?> <li> <label for="ssn" class="required"><em>*</em><?php echo $this->__('SSN') ?></label> <div class="input-box"> <input type="text" name="ssn" id="ssn" value="<?php echo $this->htmlEscape($this->getCustomer()->getSsn()) ?>" title="<?php echo $this->__('ssn') ?>" class="input-text required-entry" /> </div> </li>
Step 4: Now Open Accountcontroller.php (The actual business logic goes here)
Search for
$customer->setPassword($this->getRequest()->getPost('password'));
And replace it with
$customer->setPassword($this->getRequest()->getPost('password')); $customer->setSsn($this->getRequest()->getPost('ssn'));
Step 5:
On register.phtml on very top of the page just write following code. (This code should be written and run only one time such that it should create a new field in database. After once this code must be removed from this file)
<?php $setup = new Mage_Eav_Model_Entity_Setup('core_setup'); $setup->addAttribute('customer', 'ssn', array( 'label' => 'SSN', 'type' => 'varchar', 'input' => 'text', 'visible' => true, 'required' => true, 'position' => 1, )); ?>
Now just refresh registration page and after refreshing page delete above code (made on register.phtml)
And thats all you will see SSN # is coming on your registration page.
Chandra Shekhar
Latest posts by Chandra Shekhar (see all)
- Best practices for micro service design - January 23, 2022
- Spring Boot - January 23, 2022
- Java - January 23, 2022
Recent Comments