Home > Enterprise >  Prestashop 1.7.8.2 add country in registration form
Prestashop 1.7.8.2 add country in registration form

Time:12-30

i need to add a select with defined in admin countries to a prestashop registration form. Any hint on how to do this? Simple prestashop 1.7.8.2

Was searching the web, but no strict answers.

CodePudding user response:

You can use additionalCustomerFormFields hook that is placed inside CustomerFormatter class.

Example usage: https://github.com/PrestaShop/ps_emailsubscription/blob/dev/ps_emailsubscription.php#L1005

CodePudding user response:

Well, i managed to sort it out myself also by:

  1. Adding select with countries that are added to Presta in file /override/classes/form/CustomerFormatter.php

$countries = Country::getCountries((int)$this->language->id, true, false, false);
        if (count($countries) > 0) {
            $countryField = (new FormField)
                ->setName('id_country')
                ->setType('countrySelect')
                ->setLabel($this->translator->trans('Country', [], 'Shop.Forms.Labels'))
                ->setRequired(true);
            foreach ($countries as $country) {
                $countryField->addAvailableValue(
                    $country['id_country'],
                    $country['country']
                );
            }
            $format[$countryField->getName()] = $countryField;
        }

  1. Adding to file /override/classes/AuthController.php right below:

    if ($hookResult && $register_form->submit()) {

this code:

//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());
                    }
                }

The above code is responsible to add new address within provided data. In My case additional is only country, but if You want to add more address data the fields should be added again in CustomerFormatter.php

Credits for several parts of the code: https://prestapros.com/en/blog/additional-fields-for-registration-form-prestashop-1-7

And:

https://www.prestashop.com/forums/topic/621262-prestashop-17-add-address-in-registration-form/?do=findComment&comment=3380394

Cheers!

  • Related