PrestaCraft on Facebook

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


PrestaCraft

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.

Leave a comment

Your email address will not be published.

63 comments

Leave a comment

Your email address will not be published.