Jump to content

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


rotamota

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
Link to comment
Share on other sites

  • 2 weeks later...

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!

Link to comment
Share on other sites

  • 3 months later...
  • 5 months later...
  • 2 weeks later...

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?

Link to comment
Share on other sites

  • 5 weeks later...
  • 2 months later...

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

Link to comment
Share on other sites

  • 4 months later...

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)
Link to comment
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.

Link to comment
Share on other sites

  • 2 weeks later...

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', ...

Link to comment
Share on other sites

  • 2 weeks later...
  • 1 month later...
  • 1 month later...

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

Link to comment
Share on other sites

  • 1 month later...

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
Link to comment
Share on other sites

  • 1 month later...

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

Link to comment
Share on other sites

  • 3 weeks later...

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

Link to comment
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.

Link to comment
Share on other sites

  • 9 months later...

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

                                               ),

Link to comment
Share on other sites

  • 4 months later...

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.

Link to comment
Share on other sites

  • 8 months later...
  • 7 months later...

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

Link to comment
Share on other sites

  • 3 weeks later...
  • 2 weeks later...
  • 2 years later...
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.

 

Link to comment
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?

Link to comment
Share on other sites

  • 1 year later...

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

Link to comment
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...