How to add custom attribute to Magento’s registeration page
For demonstration, we will add a height attribute to Magento’s registeration page
1. Add following lines from 7 to 10 in app\code\core\Mage\Customer\etc\config.xml
<customer_account> ... <gender> <create>1</create> <update>1</update> </gender> <height> <create>1</create> <update>1</update> </height> ... </customer_account>
2. In app\design\frontend\default\yourtheme\customer\form\edit.phtml, find the below code
<li> <label for="email" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label> <div class="input-box"> <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text required-entry validate-email" /> </div> </li>
and then add this code snippet right after the above code
<li> <div class="input-box"> <label for="height"><?php echo $this->__('Height') ?> </label><br/> <input type="text" name="height" id="height" value="<?php echo $this->htmlEscape($this->getCustomer()->getHeight()) ?>" title="<?php echo $this->__('Height') ?>" class="input-text" /> </div> </li>
4. Add this code at the beginning in the file app\design\frontend\default\yourtheme\template\persistent\customer\form\register.phtml
<?php $setup = Mage::getModel('customer/entity_setup', 'core_setup'); $setup->addAttribute('customer', 'height', array( 'type' => 'decimal', 'label' => 'Height', 'input' => 'text', 'required' => false, 'sort_order' => 8, )); if (version_compare(Mage::getVersion(), '1.6.0', '<=')) { $customer = Mage::getModel('customer/customer'); $attrSetId = $customer->getResource()->getEntityType()->getDefaultAttributeSetId(); $setup->addAttributeToSet('customer', $attrSetId, 'General', 'height'); } if (version_compare(Mage::getVersion(), '1.4.2', '>=')) { Mage::getSingleton('eav/config') ->getAttribute('customer', 'height') ->setData('used_in_forms', array('adminhtml_customer','customer_account_create','customer_account_edit','checkout_register')) ->save(); } ?>
Find the below code
<li> <label for="email_address" class="required"><em>*</em><?php echo $this->__('Email Address') ?></label> <div class="input-box"> <input type="text" name="email" id="email_address" value="<?php echo $this->escapeHtml($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="input-text validate-email required-entry" /> </div> </li>
and then add this code snippet right after the above code
<div class="input-box"> <label for="height"><?php echo $this->__('Height') ?></label><br/> <input type="text" name="height" id="height" value="<?php echo $this->htmlEscape($this->getFormData()->getHeight()) ?>" title="<?php echo $this->__('Height') ?>" class="input-text" /> </div>
6. Reload the registration page, you will see the Height attribute showing up.
7. If you go to the Magento database, and run this query:
select * from eav_attribute where attribute_code="height";
you should see the newly added attribute “height”.
Note: The above are demonstrations for the steps needed to add a new attribute in Magento, which involved changing the Magento core files. The proper way to do it is to create a new module which won’t change Magento’s core files and does the necessary steps mentioned above for adding new attributes in Magento.
Search within Codexpedia
Search the entire web