PrestaCraft on Facebook

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


PrestaCraft

Tutorials

After quite big success of my tutorial about adding new fields to the registration form in PrestaShop 1.6 I’ve decided to create one for 1.7. There are some similarities according to 1.6 version.

Looking for PrestaShop 1.6 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: classes/form/CustomerFormatter.php

Now it’s time to edit CustomerFormatter class to make our fields visible in the registration form.

Find:

Add before:

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

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:

Save the file and go to your customer registration form page. You should see something like this:

Remeber that your customers are now able to edit values of those fields in the Information section in the My account page. In the form you can see after clicking on:

That’s enough – time for the backoffice.

BackOffice modifications

Let’s display our fields in the BackOffice > Customer Settings > Customer Settings.

First, you must just add new form fields in the Admin Customers Controller. Look at the code.

Edit: controllers/admin/AdminCustomersController.php

Find:

Add after:

Now if you go to the customer creation/edit form you are going to see two new fields:

Time to display the data in backoffice customer page.

Edit: adminXXX/themes/default/template/controllers/customers/helpers/view/view.tpl
Find:

Add after:

Now your values are visible as you can see below:

Add your new field to the BackOffice Customer table

If you wish to display your field in BackOffice Customer table (in the Customer Settings -> Customer Settings 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 you’ll see new columns in this table:

Set overrides

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


Source Destination
controllers/admin/AdminCustomersController.php override/controllers/admin/AdminCustomersController.php
classes/Customer.php override/classes/Customer.php
classes/form/CustomerFormatter.php override/classes/form/CustomerFormatter.php

2. Replace in DESTINATION files:
class AdminCustomersControllerCore extends AdminController replace with class AdminCustomersController extends AdminCustomersControllerCore
class CustomerCore extends ObjectModel replace with class Customer extends CustomerCore
class CustomerFormatterCore implements FormFormatterInterface replace with class CustomerFormatter extends CustomerFormatterCore

3. Purge cache:
Remove /app/cache/ directory.

That’s it. Thanks for reading. If you face any problem or just want to say “thank you”, leave a comment here. If you like my articles and modules, please consider donation.

Comments about this post (15)

Add a comment
mohamed 31-10-2018
this is brilliant

thanks

i have one question, why i cant add these fields in address format under the country settings ?
shadMod 14-10-2018
Fantastic!! Thanks!!
Francesco 06-10-2018
Awesome, very good tutorial! Thank you so much!
Jules 09-08-2018
This tuturial ended up being extremely helpfull, thanks a lot.

I spotter a typo though
"Remeber that your customers are now able to edit values of those fields in the Information section in the My account page. "

"Remeber"
Turpin 08-03-2018
Great thank you very clear , works like a charm.
But there is a small problem in admin customer dashboard cause it create one more in my customer table so it push "modify" column to the right and goes outside of panel col-lg-12. is there anyway to solve this ? Thank You
Jean-Pierre 20-11-2017
Problem solved... i made a mistake in a mofication by using the sign - rather than the sign _ in my field name...
Jean-Pierre 20-11-2017
Hello,
I've added a filed named "agent_id_required" as shown in this tutorial. All seems to work correctly.
However in the administration panel when i do a Manage Customer i get this error message:

Bad request SQL
Unknown column 'agent_id' in 'field list'

What could be wrong ?

Thanks for help
PierreB 23-08-2017
Very good tuto
For the translation, I don't find the field :( ...Where I can find ?
Thanks
PrestaCraft 22-08-2017
You are right, it is correct now, there was a typo in filename.
Fabio 21-08-2017
The "public $groupBox" is not in CustomerFormatter.php is in the Model Customer.php
Fabio 21-08-2017
Nice tutorial, i'm having problems saving the information in mysql database.
When i click the save button in the registration form the information is not stored in mysql, it's blank.

Do we have to clean smarty cache to this working?
daniel 20-08-2017
Having same problem as Yan.

"public $groupbox;"... is not showing up on ../customerformatter.php
Yan 19-08-2017
Also in Edit: controllers/admin/AdminCustomersController.php

Missing:

) as connect,

to add:

my_tlf_required, my_dis_required, my_dir_required, my_cps_required
Yan 19-08-2017
Hi and thank you for the guide!

Im trying to add new fields into my registration for m for new users in my store, following the next guide:

http://prestacraft.c...restashop-1-7/#


But im having some troubles with this due to some code not displayed in my CustomerFormatter.php file...or its just me or there is no such code inside as writed in that guide.

Here is the deal:


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: (THIS DOESNT EXIST)


public $groupBox;
Add after: (SO I CANT REALLY COPY PAST THIS)

public $my_field;
public $my_field_required;
Find: (THIS DOESNT EXIST)

'table' => 'customer',
'primary' => 'id_customer',
'fields' => array(
Add after: (SO I CANT REALLY COPY PAST THIS)

'my_field' => array('type' => self::TYPE_STRING),
'my_field_required' => array('type' => self::TYPE_STRING, 'required' => true),

I've created 4 new columns in my ps_customer table.

which are:
ALTER TABLE ggwplayc_customer ADD COLUMN my_tlf_required VARCHAR(250);
ALTER TABLE ggwplayc_customer ADD COLUMN my_dif_required VARCHAR(250);
ALTER TABLE ggwplayc_customer ADD COLUMN my_dir_required VARCHAR(250);
ALTER TABLE ggwplayc_customer ADD COLUMN my_cps_required VARCHAR(250);
After that Ive made and added next code as stated in the guide.

$format['my_tlf_required'] = (new FormField)
->setName('my_tlf_required')
->setLabel(
$this->translator->trans(
'Telefono', [], 'Shop.Forms.Labels'
)
)
->setRequired(true)
;
$format['my_dif_required'] = (new FormField)
->setName('my_dif_required')
->setLabel(
$this->translator->trans(
'CIF/NIE/DIF/PASAPORTE', [], 'Shop.Forms.Labels'
)
)
->setRequired(true)
;
$format['my_dir_required'] = (new FormField)
->setName('my_dir_required')
->setLabel(
$this->translator->trans(
'Direccion', [], 'Shop.Forms.Labels'
)
)
->setRequired(true)
;
$format['my_cps_required'] = (new FormField)
->setName('my_cps_required')
->setLabel(
$this->translator->trans(
'Codigo Postal', [], 'Shop.Forms.Labels'
)
)
->setRequired(true)
;
So yeah the problem is i cant find the code he is asking me to find lol in that specific .php file :(((

any ideas?
el_lo 28-07-2017
Hi,
Nice tutorial, thank you ! How can we translate the 'My field required' ?
I can't seem to find it in International / translations. I'm searching in Shop-Forms-Labels but there is nothing there. Kind regards.

Leave a comment

Your email address will not be published.