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.
I’m aware that this solution may not work for PrestaShop 1.7.6+ versions. That’s due to PrestaShop 1.7 changing architecture. If you use this version or newer, you should consider looking for a module which is reponsible for adding new fields to the registration form. I have plans to release such module, however due to my limited time I don’t know when. Thanks for understanding!
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.
ALTER TABLE ps_customer ADD COLUMN my_field VARCHAR(250); ALTER TABLE ps_customer ADD COLUMN my_field_required VARCHAR(250);
Now it’s time to edit CustomerFormatter class to make our fields visible in the registration form.
Find:
$format['email']
Add before:
$format['my_field'] = (new FormField) ->setName('my_field') ->setLabel( $this->translator->trans( 'My field', [], 'Shop.Forms.Labels' ) ) ; $format['my_field_required'] = (new FormField) ->setName('my_field_required') ->setLabel( $this->translator->trans( 'My field required', [], 'Shop.Forms.Labels' ) ) ->setRequired(true) ;
Of course, you can change the position of new fields and put it wherever you want.
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:
public $groupBox;
Add after:
public $my_field; public $my_field_required;
Find:
'table' => 'customer', 'primary' => 'id_customer', 'fields' => array(
Add after:
'my_field' => array('type' => self::TYPE_STRING), 'my_field_required' => array('type' => self::TYPE_STRING, 'required' => true),
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.
Find:
array( 'type' => 'text', 'prefix' => '', 'label' => $this->trans('Email address', array(), 'Admin.Global'), 'name' => 'email', 'col' => '4', 'required' => true, 'autocomplete' => false ),
Add after:
array( 'type' => 'text', 'label' => $this->trans('My field', array(), 'Admin.Global'), 'name' => 'my_field', 'col' => '4', ), array( 'type' => 'text', 'label' => $this->trans('My field required', array(), 'Admin.Global'), 'name' => 'my_field_required', 'required' => true, 'col' => '4', ),
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.
{if isset($customer->birthday) && $customer->birthday != '0000-00-00'} {l s='%1$d years old (birth date: %2$s)' sprintf=[$customer_stats['age'], $customer_birthday]} {else} {l s='Unknown'} {/if}
Add after:
{if isset($customer->my_field)} {$customer->my_field} {else} {l s='Unknown'} {/if}{if isset($customer->my_field_required)} {$customer->my_field_required} {else} {l s='Unknown'} {/if}
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.
'email' => array( 'title' => $this->trans('Email address', array(), 'Admin.Global') ),
Add after:
'my_field' => array( 'title' => $this->trans('My field', array(), 'Admin.Global') ), 'my_field_required' => array( 'title' => $this->trans('My field required', array(), 'Admin.Global') ),
Find:
) as connect
Add after:
,my_field, my_field_required
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.