PrestaCraft on Facebook

Please like our page on Facebook to get notifications about new tutorials :)


PrestaCraft

Tutorials

I’ve read a lot of posts (old and new) about extending PrestaShop’s registration form by adding a new field. As far as I know some people had several problems with implementing it.

Today I’m going to show you how to implement it correctly for normal and required field (so you can choose one or both, depending on your needs).

Looking for PrestaShop 1.7 version of this tutorial? Click here!
Before any changes, please remember to do a backup of all your files! I don’t take any responsibility for presumable inconveniences or bugs which may occur after those modifications.

FrontOffice modifications

Let’s start with adding new columns called my_field and my_field_required to the ps_customer table . Go to your phpMyAdmin and execute the following SQL command.

Edit: /themes/default-bootstrap/authentication.tpl

Now it’s time to edit an authentication template to make our fields visible in the registration form.

Find:

Add after:

Of course, you can change the position of new fields and put it wherever you want.

After those modifications, your registration form should look like this:

Edit: /controllers/front/AuthController.php

Next, let’s modify Auth Controller. You have to add there your new fields so it can pass their values to the Customer model and save it to database.

Find:

Add after:

Edit: /classes/Customer.php

Now, extend your Customer model by adding new columns. You can also add there a validation and determine if the field must be required.

Find:

Add after:

Find:

Add after:

Let’s see how our validation works. Type something in the My field but leave the My required field blank. Next, click the register button placed at the bottom.

Great, the validation works perfectly!
P.S. If you would like to change the error message, check out this tutorial.

Now, fill the My required field and click the register button to create an account.
Of course, it will also save the given values to the database.

That is fair enough, so let’s go to the BackOffice!

BackOffice modifications

From now users can insert values of your fields in registration form and those values are stored in the database.
But there won’t be any new data in the BackOffice > Customers > Customers.
That’s why I will show you what to do to display and edit your new data from the admin panel.

Edit: /adminXXX/themes/default/template/controllers/customers/helpers/view/view.tpl
Again, you can put those fields in another order. But it’s important to view the new data by reading $customer array which is representation of database fields values for chosen customer. Here we’ll just make a call to my_field and my_field_required fields.

Find:

Add after:

To see your changes, select a customer you’ve created and click View.

Edit: /controllers/admin/AdminCustomersController.php

It’s simple to make editing possible. You must just add new form fields in the Admin Customers Controller. Look at the code.

Find:

Add after:

That was easy, right?

Now go to the edit page of the customer you’ve created. You should see something like that:

The good thing is that values of both fields are automatically filled.

Enable editing for users

You may want to let your customers edit their data.

If so, login to your shop with new user credentials, go to “My account” and click:

That’s the form we’re going to modify.

Edit: /themes/default-bootstrap/identity.tpl
You must insert here the same code we used in authentication template (authentication.tpl).

Find:

Add after:

Add your new field to the BackOffice Customer table

If you wish to display your field in BackOffice Customer table (in the Customers -> Customers tab), you must edit AdminCustomersController and make it aware of the new field.

Edit: /controllers/admin/AdminCustomersController.php

Find:

Add after:

Find:

Add after:

Save the file and Your table should look like this:

Set overrides

Last and very important thing is to move all Your changes to override/ directory.
1. Copy and paste files:


Source Destination
controllers/front/AuthController.php override/controllers/front/AuthController.php
classes/Customer.php override/classes/Customer.php
controllers/admin/AdminCustomersController.php override/controllers/admin/AdminCustomersController.php

2. Replace in DESTINATION files:
class AuthControllerCore extends FrontController replace with class AuthController extends AuthControllerCore
class CustomerCore extends ObjectModel replace with class Customer extends CustomerCore
class AdminCustomersControllerCore extends AdminController replace with class AdminCustomersController extends AdminCustomersControllerCore

3. Purge cache:
Remove /cache/class_index.php file.

And that’s all! Thanks for reading. I hope that everything works as expected.

Comments about this post (60)

Add a comment
king 20-10-2018
Hello, you need to specify that the database fields have to be set as NULL accepted....otherwise the registration doesn't work IF the NOT required field is NOT filled by the user....thank you
macoury 06-09-2018
I've followed all steps for Front Office.
The problem is that new fields are not shown on register page https://macoury.com.br/presta/index.php?controller=authentication
But when I fill out all other fields and try to save appear this message:
There is 1 error
my_field_required is required.
I think eveything is working work except new fields are not shown.
PrestaLearner 29-08-2018
Yes, cleared cache from cache/class_index.php and B.O., but nevermind. I realised shortly after I commented what the issue was. I already had overrides and I was modifying in the theme. Sorry, my mistake, works on PS 1.6.2!

Thank you very much for this awesome tutorial!
PrestaCraft 27-08-2018
@PrestaLearner
Did you remove your cache/class_index.php file?
PrestaLearner 27-08-2018
Hi,

I experience an issue after following these steps. Everything looks right, but when I check in the DB, no values are stored. I double-checked every step.
Version: Prestashop 1.6.2.
I would really appreciate a reply,
Thank you very much,
Have a good day!
Fariba 11-07-2018
to @Mauro

I think when you add field, it's value is NULL. If you edit it to something, the identity error is gone.
Mauro 05-06-2018
After the modify, I have a problem with page identify. Error 500.

Solutions?

PrestaShop 1.6.0.13
PrestaCraft 25-05-2018
@Jakub
Did you get any error message?
Jakub 24-05-2018
Does not work on PrestaShop 1.6.1.6 - unable to create account.
Prylax 09-05-2018
Hello, Dear PrestaCraft. Please, I need you help! How I can check the new field for existing in database(version 1.6.1.18)?
I've tried to do it like in method getByEmail, approximately like that:

public function getByNickname($nickname, $ignore_guest = true)
{
$result = Db::getInstance()->getRow('
SELECT *
FROM `'._DB_PREFIX_.'customer`
WHERE `nickname` = \''.pSQL($nickname).'\'
'.Shop::addSqlRestriction(Shop::SHARE_CUSTOMER).'
'.'AND `deleted` = 0
'.($ignore_guest ? ' AND `is_guest` = 0' : ''));

if (!$result) {
return false;
}
$this->id = $result['id_customer'];
foreach ($result as $key => $value) {
if (property_exists($this, $key)) {
$this->{$key} = $value;
}
}
return $this;
}
But it didn't work.
Muhammad 02-09-2017
Hi, I want to show the fields I have added on every page "{$ cookie-> my_field}". But it does not work. can you help please?
Adi Maniur 06-07-2017
Followed the intructions, everything works great with only ONE problem:

I can't delete the data afterwards :)

I log in the account, add data to the new fields, everything ok, i edit data from the fields, everything ok, i try to delete data from a field, nothing happens, data is still there :)

Did you forgot something in the tutorial? or i did something wrong..
Cristian G 23-06-2017
Awesome tutorial man, you save my life TY :D
Adi 23-06-2017
Working on v. 1.6.1.14, just tested it. Thanks dude :)
SpotNick 07-06-2017
Works like a charm!

You save me a lot of work today, simple and working!

Thanks a lot!
alex 21-05-2017
great tutorial!!! thanks a lot!
Manoj 04-04-2017
Nice tutorial
... piff paff ... 16-02-2017
... still up-to-date ... excellent tutorial ... thank you !!!
Roke 07-12-2016
How to put a select list - combobox?
Rainer 08-11-2016
Hello, so far quite well, only I have a few errors.
The new field is displayed in the overview in the backend, but if I edit on clicks it is not to be found, in the front of the customer it is displayed but at the editing represented without value and a change is not possible the first value remains? I use Prestashop ver. 1.6.1.8. Does anyone know what can lie?

Best regards
Riccardo 27-10-2016
Hi all,

well done,

but if i want the "field required" required only in authentication.tpl and not in rapid checkout? it's possible?

many tnx!
Riccardo 27-10-2016
Congratulation for the Tutorial good job.

I have one question .... the field added will be invcluded in the invoice?

Thanks;)
Kruasan 03-10-2016
Great tutorial, woks perfect.

Many thanks and best regards!
Adamalufet 20-09-2016
Thanks for this tutorial, but i have one problem. When i try to click edit in BO, the inputs that i added in AdminCustomersController.php doesn't seem to appear but it appears when i view it.
PrestaCraft 30-08-2016
@Jovelyn
There was a comma missing. Yes, you can do this with two SQL statements as you wrote.
Jovelyn 28-08-2016
Hi, i tried your intruction but cant proceed because when I copy pasted this command to sql,

ALTER TABLE ps_customer
ADD COLUMN my_field VARCHAR(250)
ADD COLUMN my_field_required VARCHAR(250)

i receive this error

Error
SQL query:

ALTER TABLE ps_customer
ADD COLUMN my_field VARCHAR(250)
ADD COLUMN my_field_required VARCHAR(250)
MySQL said: Documentation

#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'ADD COLUMN my_field_required VARCHAR(250)' at line 3
============
is it okay if I execute it like this? I just ask someone and told me that I should execute it like this. but I am afraid to try.
he said that I should execute this first

ALTER TABLE ps_customer
ADD COLUMN my_field VARCHAR(250)
then
ALTER TABLE ps_customer
ADD COLUMN my_field_required VARCHAR(250)

Is it alright?
Sridhar 30-07-2016
P.S. Also a small suggestion for your website if i may. I think it will better & visitors will get more relevant comments if you display the newest comment at the top instead of oldest at the top(sorted from newest to oldest)
Just a piece of suggestion. ;)
Sridhar 30-07-2016
Hi,

Great Tutorial, There is just one problem.
I am trying to add a file uploader field in the form.
I have got that to work in the authentication page but it is not working for the indentity page.

i have used this code in IdentityController :
if(Tools::getValue('taxExempt'))
{
$file = $_FILES['taxExempt'];
$allowed = array('pdf', 'png', 'jpeg', 'gif', 'jpg');
$extension = pathinfo($file['name'], PATHINFO_EXTENSION);
if ( file_exists($file['tmp_name']) && in_array($extension, $allowed) )
{
$filename = uniqid()."-".basename($file['name']);
$filename = str_replace(' ', '-', $filename);
$filename = strtolower($filename);
$filename = filter_var($filename, FILTER_SANITIZE_STRING);

$file['name'] = $filename;

$uploader = new UploaderCore();
$uploader->upload($file);

$this->customer->taxExempt = $filename;
}
}

Can you suggest something i can do to fix this.
& Thank You for the wonderful tutorial.
Kenoxx 27-07-2016
Max, I found the problem. You have to clean the files on cache\smarty\compile\9c\1e\31 that file have a cache of the authentication.tpl

That should be automatically updated but doesn't work. I'm looking for a final solution
Zed 10-07-2016
tnx for sharing this amazing post
your are the best
alejandro calderon 02-07-2016
Hello and add the field in the login

email
new fiel
password


thanks for helme
Max 03-06-2016
Hi. I made all the tutorial says but I can't see any change on my prestashop. The tutorial is good explained, and I followed it, step by step, but i still see the same on my registration form. Even cleaning the chache memory i can't see changes. I don't know why. I'm using the 1.6.1.5 version in spanish. The form looks the same on chrome, firefox and ie. I have not tried on other browsers. Please help! This is frustrating...
ChrisMike 30-05-2016
Hey first of all thanks for the tutorial !
I would like to know if is possible to make a registration form that separates normal customers and company customers. Like if you press 1 than it apears only normal costumer fields and company hided but if you press 2 than only company fields are showed and normal customers not is that possible ? Sorry for my english
daredevil 25-05-2016
@Satish
I didn't do this.
Users editing did reflect changes in my db.
marotn 18-05-2016
Hi,
Thanks for this tutorial.
I have question.
If i want field like reference of order this name is code in custom.
It is hidden in form when custom create the account.
like this :
public static function generateCode()
{
$OrderMaxID = Db::getInstance()->getValue('
SELECT MAX(code)
FROM '._DB_PREFIX_.'custom ');
return str_pad((int)$last_id + 1, 8, 'CW-000000', STR_PAD_LEFT);
}
PrestaCraft 09-05-2016
@Majid
Thanks,
http://prestacraft.com/fixing-validation-field-name-in-prestashop/ :)
Majid 06-05-2016
Hi!
This is a great tutorial, thanks!
But I have a question, Is there any way to show custom error message instead of 'my_field_required is required'

Thanks
PrestaCraft 05-05-2016
@Mohammad
I haven't test it yet, but check out the first comment here. Try to update the context. Then, in any .tpl file use
{$cookie->my_field}
PrestaCraft 05-05-2016
@Mike
Hi,
You are thinking in right direction.
You should create id_company field in the Customer class (and also in database table ps_customer).
Then, you must save form data in your new table *customers_company*.
To do this, edit AuthController.php. After...
// Preparing customer
$customer = new Customer();

...use one of following database functions to insert customer company data:
Db::getInstance()->insert(); // PrestaShop insert query builder
Db::getInstance()->execute('INSERT INTO ... VALUES (...)'); // If you prefer raw SQL query
// P.S. You can of course save it to the object CustomersCompany, if You've created one

Next, fetch the latest ID from this table. You can do this using following query:
$id_company = Db::getInstance()->getValue('SELECT max(id_company) FROM customers_company'); // Return single value

And pass it to the customer field to make a connection:
$customer->id_company = $id_company;

Good luck!
Mike 04-05-2016
Hello!

I have a question, what happens if I want to add fields associated with other new tables? for example I want to add the field "id_company" and create the table customers_company with all the information of the company (name, address, et.). I know I can add all the fields in the customers table but I want to add more than 50 fields so I do not think that is a good idea.

This is a very good tutorial, thank you for sharing it with us! =)
Robert a.k.a Rho_bur on PS forum 30-04-2016
@Prestacrafts:
Thanks for this great tutorial, clears a lot of questions and ends an awful long search.
Kudos!
PrestaCraft 22-04-2016
@Sophocles
Thanks for those kind words :) Have a nice day.
Sophocles 21-04-2016
Outstanding and perfectly documented tutorial !!
Thank you.
koutak 13-04-2016
Thx. It's great!!!
sabreen 07-04-2016
great thaaaanks :) you made my day
Mohammad 31-03-2016
Hi,
How can i show value of fields in any place over website?
Malak 30-03-2016
Hi,
I'm trying to add new fields, but i don't want we see it in the backoffice, have i to modify admin pages ?

I can see fields and i add it in my database, but when i update the fields, nothing change in database, can you help me ?

Sorry for bad english, i'm french ;)
PrestaCraft 26-03-2016
@Alex
Hi.
http://doc.prestashop.com/display/PS16/Overriding+default+behaviors
If You do it correctly, overrides will work.
Alex 24-03-2016
Hi,
Thank you very much for this great tutorial, it works perfectly but i just have one question please :
How your tutorial works with the override of controllers, classes and views ?
I'm new on Prestashop.
Thanks.
Alex.
Patrice 11-03-2016
Hi,

I've done everything as mentionned above. And it works fine, values get into the database, even the display in the list (backoffice) works.
But on the BackOffice > Customers > Customers it just shows no values...


{l s='Klantnr'}


{$customer->klantnr}


Faty 11-03-2016
Hi!
This is a great tutorial, thanks!
I am trying to add another birthday field. I have created a "birthday1" date column in the customer table.
Can you help me with how I should change the authentication.tpl (and maybe also AuthController.php and customer.php).
I am a bit confused on how to deal with day, month and year inputs feed into "birthday1"
Thanks a lot in advance
PrestaCraft 09-03-2016
@jean-charles
Tutorial updated, check it out :)
jean-charles 08-03-2016
Hi!!
That's a great tutorial, i'll try it today!

How do you display "myfield" text in the full client list table?
Alvaro Fernandez 17-02-2016
Thank you very much!
Works perfectly with Prestashop 1.6.1.2
Good work : )
PrestaCraft 31-12-2015
prestacraft(at)gmail.com
Satish 23-12-2015
Hello ,

That is the paid module, Can u send me your email address, so i can send u the zip file of the module.
PrestaCraft 18-12-2015
@Satish

Passing dummy value is possible but it has to be done in one of the module files.
Can You send me a link to download this module? I'll try to troubleshoot it.
Have a good day ;)
Satish 15-12-2015
Most Welcome :).

i have used one page checkout module by peter silacy. And i have added custom required field as you have guided in the tutorial and it is working fine, i have enabled guest-checkout in my shop ,but when customer is guest it gives me custom_field_required error which is because required validation in definition of that field.

there are two ways i can solve this problem
one passing dummy value(which i m not able to implement)

second repeating the procedure for one pagecheckout module and related files and overides.

Can u suggest which one would be more better because that required field is phone number which i require on my create account form because email takes more time to contact with customers so wanted phone number as required field to contact them.But while using one pagecheckout module the create account form hook and address block both are called. so i get one phone number which is enough for me to contact. so i just require a dummy value to be passed when calling onepagecheckout module.
PrestaCraft 03-12-2015
@Satish
Thanks for Your feedback :)
Satish 30-11-2015
Everything worked fine :), only enable editing for uyser's didn't reflect changes in database. So made changes in
protected function updateContext(Customer $customer)
{
$this->context->customer = $customer;
$this->context->smarty->assign('confirmation', 1);
$this->context->cookie->id_customer = (int)$customer->id;
$this->context->cookie->customer_lastname = $customer->lastname;
$this->context->cookie->customer_firstname = $customer->firstname;
$this->context->cookie->my_field = $customer->my_field;
$this->context->cookie->my_required = $customer->my_field_required;
}

Leave a comment

Your email address will not be published.