Jump to content
rotamota

Tutorial / Add Custom Field To Customer Registration - How you found us / Store in DB / Display Result in Admin

Recommended Posts

This will show you how to add a "how you found us" field to the customer registration.
You can easily modify to have additional fields.

!!MAKE SURE YOU BACKUP BEFORE DOING ANY MODS!!

Modify themes/YOURTHEME/authentication.tpl

Add the following at approx line: 328 or where you would like it to display on the registration form.



-- Please Select One --
Google
MSN
Advertisement
Other
{l s='How did you find us?'}



<input name="foundother" type="text" id="foundother" /><label for="foundother">{l s='If found other'}



Next Modify controllers/AuthController.php

Add The following lines approx line 85 under $Customer = new Customer();

    
$foundother = $_POST['foundother'];
$foundus = $_POST['foundus'];



Add the following lines to approx lines 145 under $customer-birthday

     
$customer->foundus = $foundus;
$customer->foundother = $foundother;



Add the following lines to approx lines 178 under self::$cookie->id_customer = (int)($customer->id);

    
self::$cookie->foundus = $customer->foundus;
self::$cookie->foundother = $customer->foundother;



Add the following lines to approx lines 260 under self::$cookie->id_customer = (int)($customer->id);

        
self::$cookie->foundus = $customer->foundus;
self::$cookie->foundother = $customer->foundother;



Next Modify classes/Customer.php

Add the following lines to approx line 30 after public $id;

/** @var string Foundus */
public         $foundus;

/** @var string Foundother */
public         $foundother;



Add the following lines to approx line 128 after $fields['id_customer'] = (int)($this->id);

    
$fields['foundus'] = pSQL($this->foundus);    
$fields['foundother'] = pSQL($this->foundother);



Next you need to add the new fields to your database. Open phpmyadmin or similar and locate the ps_customer table.
You can run the following SQL to add the new fields. Or add them manually.

ALTER TABLE `ps_customer` ADD `foundus` VARCHAR( 32 ) NOT NULL ,
ADD `foundother` VARCHAR( 32 ) NOT NULL 



DISPLAYING THE DATA IN THE ADMIN


Modify **ADMIN DIRECTORY**/tabs/AdminCustomers.php

REPLACE:

'email' => array('title' => $this->l('E-mail address'), 'width' => 120, 'maxlength' => 19),



WITH:

    
'email' => array('title' => $this->l('E-mail address'), 'width' => 110, 'maxlength' => 19),
'foundus' => array('title' => $this->l('Found us'), 'width' => 20),
'foundother' => array('title' => $this->l('Found Other'), 'width' => 20),



This displays the information in the first customer view from within the admin. It also enables you to search
by foundus.

Next we want to display the information on the customer view. section

Add the following lines to approx line 273 under '.($countBetterCustomers != '-' ? $this->l('Rank: #').''.(int)$countBetterCustomers.'

' : '').'

        
'.$this->l('Found us:').' '. ($customer->foundus).'

'.$this->l('Found other:').' '. ($customer->foundother).'



Your done, Test and enjoy. :)

  • Like 8

Share this post


Link to post
Share on other sites

wow, nice, I haven't tried it yet but I will definitely. Looks like you have thought of everything. well done.

Share this post


Link to post
Share on other sites

Thank, I tried to use this way for another field (default_group_id) and is just BEAUTYFULL.
Thanks so much.

Shella

Share this post


Link to post
Share on other sites

Hi.

sorry for my English...

I use ver.1.3.2 of prestashop and i dont'have controllers/AuthController.php ...how I do?

Please help me!!!!

Share this post


Link to post
Share on other sites

AWESOME MAN!!!

That's exactly what I needed!

EDIT:

Just one thing, anybody knows how can I insert the same field in ps_address? I want to show that field with the customer address all the time.

I’ve been playing with controllers/AddressController.php and controllers/AddressesController.php but I can’t get any result…

Any ideas?

Thanks!

Share this post


Link to post
Share on other sites

Great!!!

 

Thanks very much rotamota.

 

I still have a problem, in BO tabs a zero appears in Found us and Found Other. any clue?

Share this post


Link to post
Share on other sites

Ok, i dont know why but deleting everything about foundother resolve the thing. Now i'm trying to ad and age verification fiel with a radio button like "I'm over 18" but can make it required.

Share this post


Link to post
Share on other sites

This works great on Prestashop 1.3.7.0 - how can I also show this info on the order detail tab?

Share this post


Link to post
Share on other sites

When I installed this I tested it out fully and it was working. Now when someone tries ot register, they get "an error occurred while creating your account message" - what woudl be causing this and why would it happen now when it didn't before?

Share this post


Link to post
Share on other sites

I also attempted to install this on 1.4.7.3 and get

 

"There is 1 error :

  1. An error occurred while creating your account."

I have tried to figure out what the problem is, but can't find it. Anyone have any ideas? Thanks!

Share this post


Link to post
Share on other sites

Hi, I did get it working, but honestly can't remember what I did now! I think I found it was a clash with a Facebook login module I had

Share this post


Link to post
Share on other sites

Hmmm.... I disabled all my custom modules, but that did not work. :(

 

I noticed that there is no mention to assign smarty variables here. Could that be why this isn't working?

Share this post


Link to post
Share on other sites

Problem with this tutorial is: If the user doesn't specify the foundother field. Then he gets that "can't create account message". Simply don't use the foundother field...

Share this post


Link to post
Share on other sites

Hi all....

 

thanks to Rotamota for his tutorial that solved my problem after trying a lot of time to add a custom not required field ( "website" ) in the reg. form....

 

Now is working quite fine...i inserted the field website in the form and i can see it in Bo if the user compile it....

Two problems:

1 - when the user log in and goes to his address in the account....he cannot see the website name inserted when registered

2 - if he want to modify the address, the field Website is therewithout value and if inserting a new one, this is not changing in the DB and not even in BO

 

Anyone out there has the same or similar problem and find a solution?

I use Prestashop vers.1.4.7.3

 

Thanks in advance for replying

Ariom

Share this post


Link to post
Share on other sites

Are the instructions for Prestashop 1.5 still available ? Before starting to paste all the code pieces, I tried to open all files that need changes and I noticed that a few files from 1.5 are in different paths:

 

Next Modify controllers/AuthController.php

which probably became: Next Modify controllers/front/AuthController.php

 

Modify **ADMIN DIRECTORY**/tabs/AdminCustomers.php

which probably became: Modify controllers/admin/AdminCustomersController.php

 

Also I'm not sure if other changes must be applied for 1.5 following this instructions. Anybody confirm ?

 

Thanks

Edited by badjoker (see edit history)

Share this post


Link to post
Share on other sites

Are the instructions for Prestashop 1.5 still available ? Before starting to paste all the code pieces, I tried to open all files that need changes and I noticed that a few files from 1.5 are in different paths:

 

 

which probably became: Next Modify controllers/front/AuthController.php

 

 

which probably became: Modify controllers/admin/AdminCustomersController.php

 

Also I'm not sure if other changes must be applied for 1.5 following this instructions. Anybody confirm ?

 

Thanks

 

There were definitely some significant changes from 1.4 to 1.5. I have not tried to implement this in 1.5 myself, but can confirm that things will need to be done a bit differently. Mainly due to them changing file structure, as you mentioned.

Share this post


Link to post
Share on other sites

I added 'foundother' in $fieldsRequired, $fieldSize & $fieldsValidate arrays. This worked for me under 1.4.9. For exmaple:

 

(1) protected $fieldsRequired = array('foundother', ...

 

(2) protected $fieldsSize = array('foundother' => 32, ...

 

(3) protected $fieldsValidate = array('foundother' => 'isName', ...

Share this post


Link to post
Share on other sites

also for optional fields:

 

AuthController.php:

if (Tools::getValue('foundus'))
           $foundus = $_POST['foundus'];

 

Customer.php

if (Tools::getValue('foundus'))
$fields['foundus'] = pSQL($this->foundus);

Share this post


Link to post
Share on other sites

Can anyone update this tutorial to do this in 1.5 version?? That whould be much appreciated! Thank you

Share this post


Link to post
Share on other sites

Hi All.. Any body found a solution for 1.5 ? how to add fields to the registration process? no guide so far on the internet. come on guys be the first one!!!

Edited by ricky11 (see edit history)

Share this post


Link to post
Share on other sites

Hello, maybe you would be interested. I've created module that will allow add extra fields to registration form. With that module you can easily create unlimited number of additional fields and sections on top and bottom of the registration form. The advantage of that module is that it doesn't override or change any core files. So further updates of the Prestashop to newer versions wouldn't be a problem.

 

You can check it here:

http://addons.presta...tom-fields.html

Share this post


Link to post
Share on other sites

I also attempted to install this on 1.4.7.3 and get

 

"There is 1 error :

  1. An error occurred while creating your account."

I have tried to figure out what the problem is, but can't find it. Anyone have any ideas? Thanks!

 

I had the same problem. The problem was that OP had "NONE" for default value for those added fields.

Edit the field's default value to "NULL" and also, check "NULL".

 

This way, even if the customer doesn't specify the field or if the field doesn't get initialized for any reason,

the customer can still register without any problems.

  • Like 1

Share this post


Link to post
Share on other sites

im currently adding new custom fields to users in prestashop 1.5.xx in the front view is easy....

 

only change the authentification.tpl in your theme folder and add this line:

 

<p class="text">

<label for="custom_1">{l s='custom_1'}</label>

<input type="text" class="text" name="custom_1" id="custom_1" value="" />

</p>

 

and next add this line in customer.php controller, in public static $definition = array( :

 

 

'custom_1' => array('type' => self::TYPE_STRING, 'validate' => 'isGenericName'),

 

 

the last step is add custom_1 field in ps_customer table in MYSQL

 

 

the only task that need is show fields in administration.....

 

Bye

Share this post


Link to post
Share on other sites

I have successfully done some changes in prestashop 1.5.2.0, please check the modification detail below:

 

Changing the "is required" error message

· File: ObjectModel.php

· Location: /classes/

· Search: // Checking for required fields

· To do: just change the error message text

 

Add/edit field in Create New Account form field :: Add storefront form field

· File: authentication.tpl

· Location: /themes/xxxxx/

· Search: {$HOOK_CREATE_ACCOUNT_TOP}

· To do: Create new input form

 

Add/edit field in Create New Account form field :: Connect to controllers

· File: AuthController.php

· Location: /controllers/front/

· Search: // Preparing customer

· To do: add new line, consist of your new field name, just like $_POST['lastname'] = Tools::getValue('customer_lastname');

 

Add/edit field in Create New Account form field :: Save to database

· File: Customer.php

· Location: /classes/

· Search: public static $definition = array(

· To do: at the “fields” of customer detail array, add your additional field detail – watch carefully for mandatory field, make sure you add ('required' => true) statementChanging the "is required" error message

· File: ObjectModel.php

· Location: /classes/

· Search: // Checking for required fields

· To do: just change the error message text

 

Add/edit field in Create New Account form field :: Add storefront form field

· File: authentication.tpl

· Location: /themes/xxxxx/

· Search: {$HOOK_CREATE_ACCOUNT_TOP}

· To do: Create new input form

 

Add/edit field in Create New Account form field :: Connect to controllers

· File: AuthController.php

· Location: /controllers/front/

· Search: // Preparing customer

· To do: add new line, consist of your new field name, just like $_POST['lastname'] = Tools::getValue('customer_lastname');

 

Add/edit field in Create New Account form field :: Save to database

· File: Customer.php

· Location: /classes/

· Search: public static $definition = array(

· To do: at the “fields” of customer detail array, add your additional field detail – watch carefully for mandatory field, make sure you add ('required' => true) statement

Share this post


Link to post
Share on other sites

Can anybody help me to modify the administrator page? In tab CUSTOMERS - CUSTOMERS, I have succeeded to show my additional fields. But I have 2 problems:

 

1. VIEW --> I still cannot show my additional detail in the customer's detail page

 

2. EDIT --> I already success to show my additional field and input field in edit page, but I failed to show the content of the aditional field, and also failed to save the changes

 

Please help me. Thank you in advance.

Share this post


Link to post
Share on other sites

Hello ,

 

i test solutions of

But not work for me.. this should be very simple.. only need pass value.. prestashop delete $_POST whit unset($_POST) T_T

Share this post


Link to post
Share on other sites

Hi

I'm starting with PrestaShop - so any corrections and suggestions are welcome. Even more, please take a look at my tutorial and comment.

 

I was trying to add 'sponsor email' field to registration form - sponsor is the one who recommended the shop to another customer. I start with first tutorial in this topic and made it work on ver. 1.5.6.2. - I tested it and it's working for me at front and back. But for sure there are some things to do more about it. For now I did:

1. add field to registration form - sponsor email

2. checking if the email is correct

3. checking if the sponsor's email exist in database

4. sponsor email field is set as required field

 

to do:

5. send email to sponsor with message like : "Someone name, email.. blablabla ;) used your email as a sponsor email in registration proces at 'ShopName' ".

If someone have any suggestion at  5. please write.

 

Database

Add field to table ps_customer (I added this on the end of the table);

sponsor_email; varchar(128); null(yes);

the same parameters as email but null is yes

 

For front view

File: shop/themes/’theme_name’/authentication.tpl

 

Line: 391,

after: <fieldset class="account_creation titled_box">

 

input: <h2><span>{l s='Your Sponsor email'}</span></h2>

                          <div class="row-fluid">

                                    <div class="span6">

                                                <p class="required text">

                                                           <label for="sponsor_email">{l s='Sponsor Email'} <sup>*</sup></label>

                                                           <input type="email" class="text" id="sponsor_email" name="sponsor_email" value="{if isset($smarty.post.sponsor_email)}{$smarty.post.sponsor_email}{/if}" />

                                               </p>

                                    </div>

                          </div>

 

File:  shop/controllers/front/AuthController.php

 

Line: 434,

after: $customer->birthday = (empty($_POST['years']) ? '' : (int)$_POST['years'].'-'.(int)$_POST['months'].'-'.(int)$_POST['days']);

 

input: $customer->sponsor_email = $sponsor_email;

 

Line: 393,

after: // Preparing customer

$customer = new Customer();

 

input: $sponsor_email = $_POST['sponsor_email'];

// Checked the sponsor user if exist

if (Validate::isEmail($sponsor_email = Tools::getValue('sponsor_email')) && !empty($sponsor_email))

if (!Customer::customerExists($sponsor_email))

            $this->errors[] = Tools::displayError('An account with given sponsor email has not been registered.', false);

 

Line: 696,

After:

/**

                         * Update context after customer creation

                         * @param Customer $customer Created customer

                         */

[..]

 $this->context->cookie->email = $customer->email;

 

input: $this->context->cookie->sponsor_email = $customer->sponsor_email;

 

File: shop/classes/Customer.php

Line: 31,

After: class CustomerCore extends ObjectModel

{

            public $id;

 

input: /** @var string sponsor_email */

                        public $sponsor_email;

 

Line: 168

After:              /**

             * @see ObjectModel::$definition

             */

            public static $definition = array(

                        'table' => 'customer',

                        'primary' => 'id_customer',

                        'fields' => array(

                                   'secure_key' => array('type' => self::TYPE_STRING, 'validate' => 'isMd5', 'copy_post' => false),

                                   'lastname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),

                                   'firstname' => array('type' => self::TYPE_STRING, 'validate' => 'isName', 'required' => true, 'size' => 32),

                                   'email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),

 

input:

'sponsor_email' => array('type' => self::TYPE_STRING, 'validate' => 'isEmail', 'required' => true, 'size' => 128),

 

Line: 296,

Replace:  /**

                         * Return customers list

                         *

                         * @return array Customers

                         */

                        public static function getCustomers()

                        {

                                               $sql = 'SELECT `id_customer`, `email`, `firstname`, `lastname`

                                                                                              FROM `'._DB_PREFIX_.'customer`

                                                                                              WHERE 1 '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).'

                                                                                              ORDER BY `id_customer` ASC';

                                               return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);

                        }

 

With: /**

                         * Return customers list

                         *

                         * @return array Customers

                         */

                        public static function getCustomers()

                        {

                                               $sql = 'SELECT `id_customer`, `email`, `firstname`, `lastname`, `sponsor_email`

                                                                                              FROM `'._DB_PREFIX_.'customer`

                                                                                              WHERE 1 '.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).'

                                                                                              ORDER BY `id_customer` ASC';

                                               return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);

                        }

 

For back (admin) view

File:  shop/controllers/admin/AdminCustomers.php

 

Line: 106,

after: 'email' => array(

                                               'title' => $this->l('Email address'),

                                               'width' => 140,

                                   ),

                                  

 

input: 'sponsor_email' => array(

                                                'title' => $this->l('Sponsor Email address'),

                                                'width' => 140,

                                   ),

 

Line: 305,

After: array(

                                                           'type' => 'text',

                                                           'label' => $this->l('Email address:'),

                                                           'name' => 'email',

                                                           'size' => 33,

                                                           'required' => true

                                               ),

 

input: array(

                                                           'type' => 'text',

                                                           'label' => $this->l('Sponsor Email address:'),

                                                           'name' => 'sponsor_email',

                                                           'size' => 33,

                                                           'required' => true

                                               ),

Share this post


Link to post
Share on other sites

Does anybody know if there's a module or could tell me how to add a field but the field must be an attachment? For example, when you register you have to fill all the fields and to end the register you attach a file of your ID card and I from the backoffice i can see it.

Share this post


Link to post
Share on other sites

This what I called a proper tutor, although I have yet to try the methods but reading his wiliness and helpful information I am so please to have someone like this on presta board. Well done and thank you!

Share this post


Link to post
Share on other sites

Hello all !

Thanks for this subject, it's very interesting !

 

I'd like to add a customer image (avatar like) in the registration form.
This thread talks about text data but how to manage image field upload and storage ?

If someone has already manage this it would be great sharing !

Thanks in advance for tips ^^

Best regards,
Yoann

Share this post


Link to post
Share on other sites

Hi,

 

Somebody did this on PS 1.6 ??

 

I'm stuck, i can't found any of this line :

 

Add the following lines to approx lines 178 under self::$cookie->id_customer = (int)($customer->id);

 

 

Thank you!!

jC

Share this post


Link to post
Share on other sites
On 12/24/2015 at 8:53 AM, SatishMasani said:

Hello guys a similar topic posted on these website working absolutely fine.

 

Check Out. the following URL:

 

http://prestacraft.com/adding-new-fields-to-the-registration-form/

 

Hope it helps

 

 

Hello,

we need to have a dropdown menu instead of a text box.

Can anyone help us with this?

Just need a simple dropdown with yes or no values.

 

Share this post


Link to post
Share on other sites

 

23 hours ago, Rui-PS said:

 

 

Hello,

we need to have a dropdown menu instead of a text box.

Can anyone help us with this?

Just need a simple dropdown with yes or no values.

 

 

Hello,

we need to have a dropdown menu instead of a text box.

Can anyone help us with this?

Just need a simple dropdown with yes or no values.

 

Hello,

could anyone help us with this?

Share this post


Link to post
Share on other sites

hi
I have successfully overridden customer class and added my custom fields, and they are stored in DB and showed in BO table.
problem is Custom Fields don't appear in update customer info Form in both front office and back office. is there any separated class or controller for these two forms?

I've attached two images of the forms.

 

customerPanel.jpg

addedFields.jpg

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Cookies ensure the smooth running of our services. Using these, you accept the use of cookies. Learn More