Magento Forum

Page 1 of 10
Custom Registration Fields That Insert Into The Database - SOLVED
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Alright, we will be dealing with 3 files. So open each of the following up:

app/design/frontend/default/yourstore/template/customer/form/register.html - Has the HTML form
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

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. Now, the fun part is getting that value into the database. Now, navigate to around line 78 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.

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);
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:
<?
$setup 
= new Mage_Eav_Model_Entity_Setup('core_setup'); 
     
$attr2 = array (
        
'position' => 1,
                
'is_required'=>/* 0 if you want it to be a required field, 1 if not. Also, if required, add this class to the occupation input box on register.phtml class="required-entry input-text" */
     
);
    
$setup->addAttribute('1''occupation'$attr2);
?>
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 your 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 can call this function: getOccupation. I believe this works - if not, then hopefully someone else can provide this part of the code.

Thanks all!

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Very nice writeup, that clarified a few questions I had about general architecture also.  Good job!

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Also, after doing all of this, I went into the administration to check my customer registrations. The new fields show up in the backend and are completely editable smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
alistek
Sr. Member
 
Total Posts:  293
Joined:  2008-04-02
Normal, IL
 

Now we just need to make this into a module that one can just make new attributes on the fly and it will add the code in as necessary smile

-Adam

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

that would be pretty cool smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
Mich81.com
Member
 
Total Posts:  55
Joined:  2008-04-14
 

Very nice Man.
I was looking for a way to add a checkbox to accept the condition of use of my website.
In italy you need to respect the Privacy law in order to register your customers, so I need to add a checkbox in the new account page that customer must check to agree the Privacy statement.
How can I do it?

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

I will right code for this tonight smile Thank you for your request!! Make sure to check back tomorrow

 
Magento Community Magento Community
Magento Community
Magento Community
 
i2p0
Sr. Member
 
Total Posts:  179
Joined:  2008-03-28
 

thanks to bloggleme for the great work…

i made some mods to the code for make the new field visible and editable from the user personal page, and in the backend for see the new field directly into the grid…

i’m italian my mod is for add a field called P.iva.... you need only to change the name with your field name…

first of all i have mod the file app/design/frontend/default/yourstore/template/customer/form/edit.html

adding

<div class="input-box">
            <
label for="piva"><?php echo $this->__('P.iva'?> <span class="required">*</span></label><br/>
            <
input type="text" name="piva" id="piva" value="<?php echo $this->htmlEscape($this->getCustomer()->getPiva()) ?>" title="<?php echo $this->__('P.iva') ?>" class="required-entry input-text"/>
            </
div>

after the e-mail field

then i have mod the app/design/frontend/default/yourstore/template/customer/account/dashboard/info.phtml

adding

<?php echo $this->__('P.iva : '?><?php echo $this->htmlEscape($this->getCustomer()->getPiva()) ?><br />
this allow to see the new field directly into the info dashboard… the first echo is for adding a text before of the value ofthe new field

and then i mod the app/code/core/mage/adminhtml/block/customer/grid.php

adding

$this->addColumn('piva', array(
            
'header'    => Mage::helper('customer')->__('Piva'),
            
'width'     => '100',
            
'index'     => 'piva'
        
));
this insert the new column into the backend client grid

if ou want to make the field editable from user into personal account page you have mod againapp/code/core/Mage/Customer/controllers/AccountController.php

adding near the end of the page this

->setData('piva'$this->getRequest()->getParam('piva'))
after the email set data…

i’m not a programmer i made this mods watching how are made the other field… i’t working in my site…
you can look it at
my site
if someone try it please report if i have made some mistake…
....

in italy we need to add 2 field obbligatory.... “p.iva” and “c.f”

....

to bloggleme… have you made something for the privacy chekbox???
i’m working on it to..
can i help you?

 
Magento Community Magento Community
Magento Community
Magento Community
 
i2p0
Sr. Member
 
Total Posts:  179
Joined:  2008-03-28
 

ops
there is another thing to add

in app/code/core/mage/adminhtml/block/customer/grid.php

the script before add the column, but for see the value you must also add this code in the first part of the page:

->addAttributeToSelect('piva')
 
Magento Community Magento Community
Magento Community
Magento Community
 
i2p0
Sr. Member
 
Total Posts:  179
Joined:  2008-03-28
 
Mich81.com - 10 June 2008 02:15 AM

Very nice Man.
I was looking for a way to add a checkbox to accept the condition of use of my website.
In italy you need to respect the Privacy law in order to register your customers, so I need to add a checkbox in the new account page that customer must check to agree the Privacy statement.
How can I do it?

i have the same problem..

and here is how i solved it…

first you must add a static page with the privacy statement: i have made a cms page called privacy

then you must mod the app/design/frontend/default/yourstore/template/customer/form/register.phtml

adding before the newsletter chekbox ( i have placed here)

this:

<li>
                <
input type="checkbox" name="allow_privacy" title="<?php echo $this->__('Leggere Informativa ai sensi del D.Lgs. 196/2003') ?>" value="1" id="allow_privacy" <?php if($this->getFormData()->getAllowPrivacy()): ?> checked="checked"<?php endif ?>" class="required-entry "/>
                <label for="
allow_privacy"><a href="<?php echo $this->getUrl('privacy'?>" target="_blank">Leggere Informativa ai sensi del D.Lgs. 196/2003</a><span class="required">*</span></label>
            </li>

the label is a link to the cms page that open the page in another window.... and the checkbox is a request field....

i made this modding the newsletter checkbox… it surely not the perfect way… but it works…
i think that a good programmer will clean my code from the part not necessary....

 
Magento Community Magento Community
Magento Community
Magento Community
 
bloggleme
Sr. Member
 
Avatar
Total Posts:  101
Joined:  2008-05-06
 

Sorry for the extremely late reply. As far as the checkbox situation - go to magento connect and search for “german terms and conditions” - it will pull up a mod that is great for that use. Only needs a tad bit of editing.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Zukkao
Member
 
Total Posts:  32
Joined:  2008-06-23
Bologna
 

hi guys, i’ve a problem.

I follow the tutorial and all works fine expept for 1 thing....

If my user put a string in the box of new field and, after that, he want to change that ( from his dashboard ), he can’t see first string.

From back end i can see all strings that user input from new field.

Anyone can help me?

 
Magento Community Magento Community
Magento Community
Magento Community
 
ukdazza
Guru
 
Total Posts:  308
Joined:  2008-04-16
 

Can we add a field for “customer type” and have the customer choose from a drop down box of the customer types we have for the website? This would save a lot of time putting customers manually into groups.

 
Magento Community Magento Community
Magento Community
Magento Community
 
i2p0
Sr. Member
 
Total Posts:  179
Joined:  2008-03-28
 

I’m modding this hack for the beta1.1
the new version will contain some new field in registration form, but in many case this is not enouth.
for example in Italy we need a field for the fiscal personal code.
i modded the registration field for a new input box called “cf”

here there’s how i did it:

http://www.magentocommerce.com/boards/viewreply/44793/

but i have a problem!!!

i have modified the field and the input box, but i need to connect it to the database adding the new field…

the method proposed by bloggleme doesn’t work anymore… i need a new code, but I’m not a good programmer..
please can someone help me connecting the code with the database??

thanks

 
Magento Community Magento Community
Magento Community
Magento Community
 
i2p0
Sr. Member
 
Total Posts:  179
Joined:  2008-03-28
 

in order to add the new field into the database i have used this code:

<?
$setup 
= new Mage_Eav_Model_Entity_Setup('core_setup'); 
     
$attr2 = array (
        
'position' => 1,
                
'is_required'=>/* 0 if you want it to be a required field, 1 if not. Also, if required, add this class to the occupation input box on register.phtml class="required-entry input-text" */
     
);
    
$setup->addAttribute('1''cf'$attr2);
?>

but it wasn’t enouth.... i have manually modd the frontend label to “C.F.”
how can i say to write it into the database into the code written before??

 
Magento Community Magento Community
Magento Community
Magento Community
 
exsso
Member
 
Avatar
Total Posts:  51
Joined:  2008-08-06
Singapore
 

Hi all,

Where can I find this block of codes? I like to remove some fields that are required for registration and just can’t seem to get rid of the required field after following the steps above.

Also, I am assuming the instructions above will apply to if you want to remove a filed for registration and not add to it.

i2p0 - 20 July 2008 11:15 AM

in order to add the new field into the database i have used this code:

<?
$setup 
= new Mage_Eav_Model_Entity_Setup('core_setup'); 
     
$attr2 = array (
        
'position' => 1,
                
'is_required'=>/* 0 if you want it to be a required field, 1 if not. Also, if required, add this class to the occupation input box on register.phtml class="required-entry input-text" */
     
);
    
$setup->addAttribute('1''cf'$attr2);
?>

but it wasn’t enouth.... i have manually modd the frontend label to “C.F.”
how can i say to write it into the database into the code written before??
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 10