Additional fields for registration form PrestaShop 1.7


In PrestaShop 1.7 adding form fields to registration form has been removed. In this short tutorial we will show how to deal with this problem by adding extra address form inputs.

additional_form_fields_prestashop

Please remember that you take all the risk by changing a code in your shop. Despite overriding was deeply limited from PrestaShop 1.7 we will use this catalog to make changes.

First of all we will add additional form fields. To do so, add a new php file CustomerFormatter in override/classes/form catalogue. If the file already exists, just modify it. Inside this file add a new extending class:

class CustomerFormatter extends CustomerFormatterCore
{
    
}

Copy all content from original file. To extend form with a new fields we will have to add new lines of code to a $format array. In our case we will add all required fields from address form. You can also add other address inputs.

//additional fields
        $format['phone'] = (new FormField)
            ->setName('phone')
            ->setLabel(
                $this->translator->trans(
                    'Phone', [], 'Shop.Forms.Labels'
                )
            )
            ->setRequired(true)
        ;
        $format['address1'] = (new FormField)
            ->setName('address1')
            ->setLabel(
                $this->translator->trans(
                    'Address', [], 'Shop.Forms.Labels'
                )
            )
            ->setRequired(true)
        ;
        $format['postcode'] = (new FormField)
            ->setName('postcode')
            ->setLabel(
                $this->translator->trans(
                    'Zip/Postal Code', [], 'Shop.Forms.Labels'
                )
            )
            ->setRequired(true)
        ;
        $format['city'] = (new FormField)
            ->setName('city')
            ->setLabel(
                $this->translator->trans(
                    'City', [], 'Shop.Forms.Labels'
                )
            )
            ->setRequired(true)
        ;

To see changes you will have to clean cache which can be done in your backoffice. Now you can see additional form fields in registration form.

additional_form_fields_prestashop

Now we will have to deal with saving extra inputs. To do so add a new php file (if it does not exist) called AuthController in  override/classes catalogue.

class AuthController extends AuthControllerCore
{
    
}

Next copy initContent() method from original file. Below if condition that is responsible for saving registration data (($hookResult && $register_form->submit())) add this lines of code:

if ($hookResult && $register_form->submit()) {
                    
                    //address saving
                    $customer = new Customer();
                    $customer = $customer->getByEmail($register_form->getCustomer()->email);
                    
                    $address = new Address(
                        null,
                        $this->context->language->id
                    );                    

                    $address->id_country = (int) Tools::getCountry();
                    $address->address1 = Tools::getValue('address1');
                    $address->postcode = Tools::getValue('postcode');
                    $address->city = Tools::getValue('city');
                    $address->phone = Tools::getValue('phone');
                    
                    $address->firstname = $customer->firstname;
                    $address->lastname = $customer->lastname;
                    $address->id_customer = (int) $customer->id;
                    
                    $address->id_state = 0;
                    $address->alias = $this->trans('My Address', [], 'Shop.Theme.Checkout');                    
                    
                    if($address->save()){
                        $should_redirect = true;
                    } else {
                        $customer->delete();
                        $this->errors[] = $this->trans('Could not update your information, please check your data.', array(), 'Shop.Notifications.Error');
                        $this->redirectWithNotifications($this->getCurrentURL());
                    }
                }

In case when address can not be saved, new user is removed and notification is displayed. When everything goes well a new user with an address is added.

additional_form_fields_prestashop

Please keep in mind that in this tutorial a few simplifications were made. We did not use any advanced validation besides the fact that fields are required. Also country id is pulled out from a configuration. Therefore you can attempt to write a few more lines of code.

 

In order to ensure maximum convenience to users when using the website, this page uses cookie files. Detailed information is available in our Privacy Policy. Click " I agree", so that this information is no longer shown