Custom Account/Registration Fields
[Update 3 Oct 2008 - Fibo]Completed on how to display the info in the Edit Form –Note: done on the old 1.1.3, your mileage may vary[/Update 3 Oct 2008 - Fibo]
[Update 30 Aug 2008 - AlexSz]Info on how to make this work at the end of this post.[/Update 30 Aug 2008 - AlexSz]
Alright, we will be dealing with 5 files. So open each of the following:
app/design/frontend/default/yourtheme/template/customer/form/register.phtml - Has the HTML form for initial registration Note: this file will also be used for a one-shot database extension
app/design/frontend/default/yourtheme/template/customer/form/edit.phtml - Has the HTML form for edit by customers
app/code/core/Mage/Customer/Model/Entity/Setup.php - Has an array full of customer attributes to use for registration
app/code/core/Mage/Customer/controllers/AccountController.php - Has a specific block of code for registration - this is no more needed
app/code/core/Mage/Customer/etc/config.xml - Has a description of fields to be used in the forms
Alright, for this little tutorial, we will do a couple of fields. The first one will be a text box asking the customer for their occupation/title. Navigate to somewhere around line 54 of register.html - You should see this block of code:
- <li>
- <div class="input-box">
- <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
- <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
- </div>
- </li>
Now, we need to add our code for the occupation text box, so change that block of code to the following:
- <li>
- <div class="input-box">
- <label for="email_address"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br/>
- <input type="text" name="email" id="email_address" value="<?php echo $this->htmlEscape($this->getFormData()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="validate-email required-entry input-text" />
- </div>
- <div class="input-box">
- <label for="occupation"><?php echo $this->__('Occupation/Title') ?></label><br/>
- <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getFormData()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
- </div>
- </li>
Ok, if you refresh your create account (register) page, you will see the new field show up.
Do a similar thing in edit.phtml, replacing getFormData by getCustomer. In my 1.1.3 version, this is at line 32. Old code:
- <li>
- <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
- </li>
- <li>
- <div class="input-box">
- <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
- <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
- </div>
- </li>
New code:
- <li>
- <?php echo $this->getLayout()->createBlock('customer/widget_name')->setObject($this->getCustomer())->toHtml() ?>
- </li>
- <li>
- <div class="input-box">
- <label for="email"><?php echo $this->__('Email Address') ?> <span class="required">*</span></label><br />
- <input type="text" name="email" id="email" value="<?php echo $this->htmlEscape($this->getCustomer()->getEmail()) ?>" title="<?php echo $this->__('Email Address') ?>" class="required-entry validate-email input-text" />
- </div>
- </li>
- <li>
- <div class="input-box">
- <label for="occupation"><?php echo $this->__('Occupation') ?> </label><br/>
- <input type="text" name="occupation" id="occupation" value="<?php echo $this->htmlEscape($this->getCustomer()->getOccupation()) ?>" title="<?php echo $this->__('Occupation') ?>" class="input-text" />
- </div>
- </li>
Now, the fun part is getting that value into the database. Now, navigate to around line 93 in Setup.php - you should see this block of code:
- 'email' => array(
- 'type' => 'static',
- 'label' => 'Email',
- 'class' => 'validate-email',
- 'sort_order' => 6,
- ),
Now, we need to add our attribute to this file. So, change this block of code to:
- 'email' => array(
- 'type' => 'static',
- 'label' => 'Email',
- 'class' => 'validate-email',
- 'sort_order' => 6,
- ),
- 'occupation' => array(
- 'label' => 'Occupation',
- 'required' => false,
- 'sort_order' => 7,
- ),
Right under your occupation array, you will see the group_id one, etc. You need to increment the sort_order as they were doing before. So, since the sort_order for occupation is 7, make the one after it 8, and so on.
This part is needed no more, left here for possible reference - should be deleted at the end of October 2008 |
Now, head to line 164 of AccountController.php. You should see this block of code:
- $customer = Mage::getModel('customer/customer')
- ->setFirstname($this->getRequest()->getPost('firstname'))
- ->setLastname($this->getRequest()->getPost('lastname'))
- ->setEmail($this->getRequest()->getPost('email'))
- ->setPassword($this->getRequest()->getPost('password'))
- ->setConfirmation($this->getRequest()->getPost('confirmation'))
- ->setId(null);
We need to add our new occupation attribute to this, so change the code to this:
- $customer = Mage::getModel('customer/customer')
- ->setFirstname($this->getRequest()->getPost('firstname'))
- ->setLastname($this->getRequest()->getPost('lastname'))
- ->setEmail($this->getRequest()->getPost('email'))
- ->setOccupation($this->getRequest()->getPost('occupation')) //Add this
- ->setPassword($this->getRequest()->getPost('password'))
- ->setConfirmation($this->getRequest()->getPost('confirmation'))
- ->setId(null);
End of the unneeded code |
Now, our code is all set up and ready. However, we still need to add this attribute to the eav_attribute table in your mysql database. How do we do that? This block of code:
- <?php
- $setup = new Mage_Eav_Model_Entity_Setup('core_setup');
- $AttrCode = 'ocupation';
- $settings = array (
- 'position' => 1,
- 'is_required'=> 0
- );
- $setup->addAttribute('1', $AttrCode, $settings);
- ?>
My suggestion is to add this block of code to the very top of your register.html file. You only need to load this block of code once, so add it to your register.html file, navigate to your register.html file, and then remove the block of code. If you go to your mysql database, view the eav_attribute table and do a search for occupation, you will see that your attribute has been added. Note: when doing all that is described here on my 1.1.3 version, the fields I was including got the “required” flag; I manually edited it in the eav_attribute table – Fibo
Now, go back to your create account page, fill it out, including your occupation, and bam you’re finished. If you need proof that it was added to the database, simply go to the customer_entity_varchar table and look for yourself smile
Also, to retrieve the customer data (as if you were in the account dashboard or something) you call this function: getOccupation.
Written by: Chris Woodard
[Update 30 Aug 2008 - AlexSz] In order to insert the values from custom registration fields you have to edit a .xml file: app/code/core/Mage/Customer/etc/config.xml Inside this file you will find the tag <fieldsets>. To get this to work you have to add the field here. I’ve added the line:
- <occupation><create>1</create><update>1</update></occupation>
I’m attaching an excerpt of this file below. Also, you don’t need to edit the AccountController.php as the Mangento team apparently made that part of the code more generic (it reads the fields from the config.xml file).
- <fieldsets>
- <customer_account>
- <prefix><create>1</create><update>1</update><name>1</name></prefix>
- <firstname><create>1</create><update>1</update><name>1</name></firstname>
- <middlename><create>1</create><update>1</update><name>1</name></middlename>
- <lastname><create>1</create><update>1</update><name>1</name></lastname>
- <suffix><create>1</create><update>1</update><name>1</name></suffix>
- <email><create>1</create><update>1</update></email>
- <password><create>1</create></password>
- <confirmation><create>1</create></confirmation>
- <dob><create>1</create><update>1</update></dob>
- <taxvat><create>1</create><update>1</update></taxvat>
- <occupation><create>1</create><update>1</update></occupation>
- </customer_account>
- </fieldsets>
[/Update 30 Aug 2008 - AlexSz]




