Thursday, 19 September 2013

Creating extra fields in registration form in magento

Link:- http://www.webtechnologycodes.com/creating-extra-fields-in-registration-form-in-magento/


Code:- Step1.
Open your register.phtml file in magento.
if you want to add such as office address fields in your form
then insert this line


<div class="field">
                    <label for="offaddress" class="required"><em>*</em><?php echo Mage::helper('contacts')->__('Office Address') ?></label>
                    <div class="input-box">
                        <input name="offaddress" id="offaddress" title="<?php echo Mage::helper('contacts')->__('Office Address') ?>" value="<?php echo $this->escapeHtml($this->getFormData()->getOffaddress()) ?>" class="input-text required-entry" type="text" />
     </div>
            </div>



Step2.

Now to retrieve this field in my account page 

<li>
            <label for="offaddress" class="required"><em>*</em><?php echo $this->__('Office Address') ?></label>
            <div class="input-box">
               <input type="text" name="offaddress" id="offaddress" value="<?php echo $this->htmlEscape($this->getCustomer()->getOffaddress()) ?>" title="<?php echo $this->__('Office Address') ?>" class="input-text required-entry validate-email" />
            </div>
        </li>


Step 3. Now the final step is to create that type of attribute in database

for this run this script once in your register.phtml file and you are all done.


<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('customer', 'offaddress', array(
 'label'  => 'Office Address',
 'type'  => 'varchar',
 'input'  => 'text',
 'visible' => true,
 'required' => false,
 'position' => 1,
 ));
$eavConfig = Mage::getSingleton('eav/config');
$attribute = $eavConfig->getAttribute('customer', 'offaddress');
$attribute->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit'));  //enable all action
//$attribute->setData('used_in_forms', array('adminhtml_customer'));  //to make the attribute can be created in backend only
//$attribute->setData('used_in_forms', array('customer_account_create')); //to make the attribute can be created in registration only
//$attribute->setData('used_in_forms', array('customer_account_edit')); // to make the attribute can be edited in the frontend only

$attribute->save();
?>

1 comment: