Magento Resource | E-Commerce Generation Explorer

Oct/09

26

Custom Account/Registration Fields

I am busy on a big project about Magento.
I will update the blog as my work notes.

We will add four fields into Account/Registration Page.

Four core files:

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/etc/config.xml – Has a description of fields to be used in the forms

app/code/core/Mage/Customer/controllers/AccountController.php – For checkbox option

On the Register.html:
For Title:

1
2
3
4
5
6
7
8
9
10
11
12
<li>
<label for="prefix"><?php echo $this->__('Title') ?><span class="required">*</span></label><br />
<select name="prefix" id="prefix" value="<?php echo $this->htmlEscape($this->getFormData()->getPrefix()) ?>">
<option value="<?php echo $this->__('Mrs') ?>"><?php echo $this->__('Mrs') ?></option>
<option value="<?php echo $this->__('Mr') ?>"><?php echo $this->__('Mr') ?></option>
<option value="<?php echo $this->__('Ms') ?>"><?php echo $this->__('Ms') ?></option>
<option value="<?php echo $this->__('Miss') ?>"><?php echo $this->__('Miss') ?></option>
<option value="<?php echo $this->__('Dr') ?>"><?php echo $this->__('Dr') ?></option>
<option value="<?php echo $this->__('Lady') ?>"><?php echo $this->__('Lady') ?></option>
<option value="<?php echo $this->__('Lord') ?>"><?php echo $this->__('Lord') ?></option>
</select>
</li>

For Source:

1
2
3
4
5
6
7
8
9
10
11
12
13
<li>
<label for="source"><?php echo $this->__('How did you hear about us?') ?><span class="required">*</span></label><br />
<select name="source" id="source" value="<?php echo $this->htmlEscape($this->getFormData()->getSource()) ?>" >
<option value="please click here to select"><?php echo $this->__('please click here to select') ?></option>
<option value="source1"><?php echo $this->__('Friends recommendation') ?></option>
<option value="source2"><?php echo $this->__('Newspaper/ Magazine feature') ?></option>
<option value="source3"><?php echo $this->__('Browsing the web') ?></option>
<option value="source4"><?php echo $this->__('Received a xxxx catalogue in the past') ?></option>
<option value="source5"><?php echo $this->__('Existing customer from the xxxx boutique') ?></option>
<option value="source6"><?php echo $this->__('Existing online xxx customer') ?></option>
<option value="source7"><?php echo $this->__('Other') ?></option>
</select>
</li>

For Promote:

1
2
3
4
<li>
<input type="checkbox" name="is_promoted" title="<?php echo $this->__("I would love to receive xxx's mail order catalogue.") ?>" value="1" id="is_promoted" checked="checked" class="checkbox" />
 <label for="is_promoted"><?php echo $this->__("I would love to receive xxxx's mail order catalogue.") ?></label>
</li>

For Rent:

1
2
3
4
<li>
<input type="checkbox" name="is_rented" title="<?php echo $this->__('I would love to receive updates from third party sources about their products and offers.') ?>" value="1" id="is_rented" class="checkbox" />
<label for="is_rented"><?php echo $this->__('I would love to receive updates from third party sources about their products and offers.') ?></label>
 </li>

Edit.phtml
For Title:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<li>
<label for="prefix"><?php echo $this->__('Title') ?><span class="required">*</span></label><br />
<select name="prefix" width="100px" id="prefix">
<option value="<?php echo $this->htmlEscape($this->getCustomer()->getPrefix()) ?>" selected><?php echo $this->htmlEscape($this->getCustomer()->getPrefix()) ?></option>
<?php if ($this->htmlEscape($this->getCustomer()->getPrefix()) <> $this->__('Mrs') )
echo "<option value='" . $this->__('Mrs') . "'>" . $this->__('Mrs') . "</option> ";?>
<?php if ($this->htmlEscape($this->getCustomer()->getPrefix()) <> $this->__('Mr') )
echo "<option value='" . $this->__('Mr') . "'>" . $this->__('Mr') . "</option> ";?>
<?php if ($this->htmlEscape($this->getCustomer()->getPrefix()) <> $this->__('Ms') )
echo "<option value='" . $this->__('Ms') . "'>" . $this->__('Ms') . "</option> ";?>
<?php if ($this->htmlEscape($this->getCustomer()->getPrefix()) <> $this->__('Miss') )
echo "<option value='" . $this->__('Miss') . "'>" . $this->__('Miss') . "</option> ";?>
<?php if ($this->htmlEscape($this->getCustomer()->getPrefix()) <> $this->__('Dr') )
echo "<option value='" . $this->__('Dr') . "'>" . $this->__('Dr') . "</option> ";?>
<?php if ($this->htmlEscape($this->getCustomer()->getPrefix()) <> $this->__('Lady') )
echo "<option value='" . $this->__('Lady') . "'>" . $this->__('Lady') . "</option> ";?>
<?php if ($this->htmlEscape($this->getCustomer()->getPrefix()) <> $this->__('Lord'))
echo "<option value='" . $this->__('Lord') . "'>" . $this->__('Lord') . "</option> ";?>
</select>
</li>

For Promote:

1
2
3
4
<li>
<input type="checkbox" name="is_promoted" title="<?php echo $this->__("I would love to receive xxxx's mail order catalogue.") ?>" id="is_promoted" <?php if($this->htmlEscape($this->getCustomer()->getis_promoted())== '1'): ?>checked="checked"<?php endif; ?> value="1" class="checkbox" />
<label for="is_promoted"><?php echo $this->__("I would love to receive xxxx's mail order catalogue.") ?></label>
</li>

For Rent:

1
2
3
4
<li>
<input type="checkbox" name="is_rented" title="<?php echo $this->__('I would love to receive updates from third party sources about their products and offers.') ?>" id="is_rented" <?php if($this->htmlEscape($this->getCustomer()->getis_rented())== '1'): ?>checked="checked"<?php endif; ?> value="1" class="checkbox" />
<label for="is_rented"><?php echo $this->__('I would love to receive updates from third party sources about their products and offers.') ?></label>
</li>

Setup.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
					    'source' => array(
					    'label'         => 'Source',
					    'required'      => true,
					    'sort_order'    => 65,
					),
 
                    'is_promoted' => array(
					     'label'		=> 'Is_promoted',
					     'required'		=> false,
					     'sort_order'	=> 66,
					),
 
					'is_rented' => array(
					     'label'		=> 'Is_rented',
					     'required'		=> false,
					     'sort_order'	=> 67,
					),

On the Register.html TOP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$AttrCode = 'source';
$settings = array (
    'source' => 1,
    'is_required'=> 0
);
$setup->addAttribute('1', $AttrCode, $settings);
?>
 
<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$AttrCode = 'is_promoted';
$settings = array (
    'is_promoted' => 1,
    'is_required'=> 0
);
$setup->addAttribute('1', $AttrCode, $settings);
?>
<!--Go to MySQL change to INT-->
 
<?php
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$AttrCode = 'is_rented';
$settings = array (
    'is_rented' => 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.

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

On the config.xml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
        <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>
				<source><create>1</create><update>1</update></source>
                <is_promoted><create>1</create><update>1</update></is_promoted>
                <is_rented><create>1</create><update>1</update></is_rented>
				<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>
                <gender><create>1</create><update>1</update></gender>
            </customer_account>
        </fieldsets>

On the AccountController.php add:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
            /**
             * we would like to preserver the existing group id
             */
            if ($this->_getSession()->getCustomerGroupId()) {
                $customer->setGroupId($this->_getSession()->getCustomerGroupId());
            }
 
            /**
             * Custom coding for Promoted Update & Rented Update by Leo
             */
            if ($this->getRequest()->getParam('is_promoted')) {
                $customer->setis_promoted(1);
            } else {
                $customer->setis_promoted(0);
            }
 
            if ($this->getRequest()->getParam('is_rented')) {
                $customer->setis_rented(1);
            } else {
                $customer->setis_rented(0);
            }
            /**
             * End
             */

DONE!

·

18 comments

  • balaji · 19/12/2009 at 11:55

    Thank you very much. its working now.

  • Mohsin · 03/05/2010 at 12:41

    Awesome tutorial
    very comprehensive
    and easy to follow…

    If you could tell me how to apply custom validation to any field?

  • Admin comment by admin · 05/05/2010 at 10:49

  • Wiktor · 07/05/2010 at 08:41

    Could you help me with adding a custom select field in the account registration form?

    First of all – I would like to use Mage_Eav_Model_Entity_Attribute_Source_Boolean as a source for one of the fields and then create my own source. My problem is that I cannot find a way to embed the options into the template and I couldn’t find any info about that anywhere.

    I would really appreciate your help!

  • Mohsin · 24/05/2010 at 08:05

    How can I add a new attribute in Category Add form.
    e.g. I want to upload three images for a category.

  • Bizboss, · 22/12/2010 at 10:56

    Hello,
    I had at the backend a text field with 0 or 1 as value, but I would like to have also a checkbox as in the frontend.
    How to change the code for this purpose ?
    Thanks.

  • Jim Rowe · 20/01/2011 at 16:38

    I agree, really good tutorial – however while implementing the title field, I happily spotted mention of prefix and a check of showPrefix() in the template/customer/widget/name.phtml

    Some further investigation and you can actually do this system->configuration->customers->customer configuration->name and address options.

    Might be a more recent version though.

  • Hridaya · 14/02/2011 at 08:12

    hello,

    The value is not inserted in customer_entity_varchar table whether the there is items on eav_attribute table.

    So, i am using magento 1.4.2

    I think
    the code on AccountController.php is not work for me can you guide me please.

    /**
    * we would like to preserver the existing group id
    */
    if ($this->_getSession()->getCustomerGroupId()) {
    $customer->setGroupId($this->_getSession()->getCustomerGroupId());
    }

    /**
    * Custom coding for Promoted Update & Rented Update by Leo
    */
    if ($this->getRequest()->getParam(‘is_promoted’)) {
    $customer->setis_promoted(1);
    } else {
    $customer->setis_promoted(0);
    }

    if ($this->getRequest()->getParam(‘is_rented’)) {
    $customer->setis_rented(1);
    } else {
    $customer->setis_rented(0);
    }
    /**
    * End
    */

  • Sakshi · 03/05/2011 at 06:54

    Hi,

    Thanks for the code. It’s resolved my issues.
    Gr8!!!
    🙂

  • Alok · 03/05/2011 at 12:32

    i am trying to add custom fields in registration form of Magento 1.5.1.0-rc1 but its not working, My same code is working in 1.4.
    Please guide me

  • sandip · 07/06/2011 at 12:45

    Can it work on magento 1.4.1.1..

    Please , it’s urgent

  • Imran · 03/08/2011 at 11:34

    Hi! Need ur help Instead of editing core files of magento i want to perform same operation by creating overriding the base classes by creating a custom module can you tell me how can i do this?????

  • Imran · 08/08/2011 at 14:07

    Hi! I’m able to create the custom fields on my registration page but the values are not getting saved in the backend can some one tell me why is it so….
    I’m using Magento 1.5.1.0 version.

    *__(‘Employee Id’) ?>

    <input type="text" name="employee_id" id="employee_id" value="htmlEscape($this->getFormData()->getEmployee_Id()) ?>” title=”__(‘Employee Id’) ?>” class=”input-text validate-employee_id required-entry”/>

  • Miguel · 07/09/2011 at 09:53

    Hi,

    Greats from Spain, i buy modify registration form in magento 1.6, to add select to indicates a group user.

    For example:

    Select your user group:

    1. General
    2. Happy Users
    3. Big Users

    Sorry, my english is bad.

    This groups setted in database.

    Greats!

  • Anusha · 25/01/2012 at 12:45

    Am not able to set visibility for a custom attribute store wise. Any help would be great.

  • man_in_black · 19/02/2012 at 10:14

    great tutorial. can you please add a tutorial how to add customer photo even only for the edit it would be really helpful

  • jey · 06/03/2012 at 17:05

    Dont seems to work with Magento ver. 1.5.1.0? or am i doing anything wrong?

  • Rajasingh · 13/03/2012 at 12:19

    I need a clarification in customer creation at admin side.

    Is there any possibilities to load form fields based on the customer type, while creating customer account in back-end?.

    For example,

    if I select customer type as General, below fields should be displayed,

    General Name:

    General First-name and so…………..

    if I select customer type as Retailer, below fieds should be displayed,

    Retailer Name:

    Retailer First-name and so……

    Thanks in advance…..

Leave a Reply

<<

>>

Theme Design by devolux.nh2.me