Jump to content

Prestashop 1.7 add address in registration Form


claudiaIta

Recommended Posts

I hope someone could help with this:

 

after setting up Prestashop 1.7, i saw that diffrently from PS 1.6 ther's no "backoffice>customer> registration process type (standard account creation and address creation)", as in need customer fill its address already in the registration form.

 

How can i merge the address form in the registration form??

 

 

Thanks!

  • Like 2
Link to comment
Share on other sites

  • 1 month later...

I have the same problem, I need to customers who register on the website to enter all your data, including address. The version of prestashop 1.7 does not allow it. is there any way to perform a full data record? It should be as the record standard in prestashop 1.6

Link to comment
Share on other sites

  • 1 month later...
  • 8 months later...
  • 2 weeks later...
  • 2 weeks later...

@PrestaPros 

Thanks for this tutorial the only problem with it is how to disable these fields when the user is registering on the checkout page or when editing the identity

Meaning these fields should be only displayed when the user is registering for the first time only!

Link to comment
Share on other sites

I managed to find a solution by following your instruction but only changing the required to false in case the user wants to edit their personal information.

then in the customer-form.tpl file inside the foreach I added this code
 

        {$address_fields = ['phone','address1','postcode','city']}
        {if $customer.is_logged OR $page.page_name == 'checkout'}
          {if in_array($field.name, $address_fields)}
            {continue}
          {/if}
        {else}
          {if in_array($field.name, $address_fields)}
            {$field.required = true}
          {/if}
        {/if}

 

 

 

 

Link to comment
Share on other sites

I managed to add the country field through smarty 

Make sure to change in the AuthController.php  to  $address->id_country = Tools::getValue('id_country');

 

        {if $field.name == 'id_country'}
          {$field.type = 'countrySelect'}
          {assign var='countries' value=Country::getCountries((int)$language.id, true)}
          {foreach from=$countries key=k item=v}
            {$field.availableValues.$k = $v.name}
          {/foreach}
        {/if}

 

 

Edited by rygar (see edit history)
  • Like 1
Link to comment
Share on other sites

  • 2 months later...

Hi,

 

I am Thilak Developer. I have implemented this part for my one of the customer.

$customer = new Customer();
                $customer = $customer->getByEmail($this->getValue('email'));
                $address = new Address(
                        null,
                        $this->context->language->id
                    );                    

                $address->address1 = $this->getValue('address1');
                $address->postcode = $this->getValue('postcode');
                $address->city = $this->getValue('city');
                $address->phone = $this->getValue('phone');
                
                $address->firstname = $customer->firstname;
                $address->lastname = $customer->lastname;
                $address->id_customer = (int) $customer->id;
                $address->id_country = $this->getValue('id_country');
                
                $address->id_state = 0;
                $address->alias = 'My Address';                    
                $address->save();

 

 

I added the above code in ContactForm submit event.

 

 

If i want to do this in your site please contact me. But you have to buy me.

 

Thank you

 

Link to comment
Share on other sites

  • 4 months later...
  • 5 months later...

add this code in CustomerFormatter.php

$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)
                ;
                $format['vat_number'] = (new FormField)
                    ->setName('vat_number')
                    ->setLabel(
                        $this->translator->trans(
                            'Vat Number', [], 'Shop.Forms.Labels'
                        )
                    )
                    ->setRequired(true)
                ;

--> add this code in AuthController.php

inside  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->vat_number = Tools::getValue('vat_number');
                    
                    $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());
                    }
                    
                   // $should_redirect = true;

for Better please visit link 

 

Edited by jayesh_naghera (see edit history)
Link to comment
Share on other sites

  • 2 weeks later...

Hi i'm having a problem with this code. I managed to make the address fields appear in the form but i can't get it to save them. I get a 500 error.  Could you please see if i'm doing something wrong?

Here is my AuthController.php override

 

class AuthController extends AuthControllerCore
{
    public $ssl = true;
    public $php_self = 'authentication';
    public $auth = false;

    public function checkAccess()
    {
        if ($this->context->customer->isLogged() && !$this->ajax) {
            $this->redirect_after = ($this->authRedirection) ? urlencode($this->authRedirection) : 'my-account';
            $this->redirect();
        }

        return parent::checkAccess();
    }

    public function initContent()
    {
        $should_redirect = false;

        if (Tools::isSubmit('submitCreate') || Tools::isSubmit('create_account')) {
            $register_form = $this
                ->makeCustomerForm()
                ->setGuestAllowed(false)
                ->fillWith(Tools::getAllValues());

            if (Tools::isSubmit('submitCreate')) {
                $hookResult = array_reduce(
                    Hook::exec('actionSubmitAccountBefore', array(), null, true),
                    function ($carry, $item) {
                        return $carry && $item;
                    },
                    true
                );
                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());
                    }
                }
                    
                
                {
                    
                    $should_redirect = true;
                }
            }
            

            $this->context->smarty->assign([
                'register_form' => $register_form->getProxy(),
                'hook_create_account_top' => Hook::exec('displayCustomerAccountFormTop'),
            ]);
            $this->setTemplate('customer/registration');
        } else {
            $login_form = $this->makeLoginForm()->fillWith(
                Tools::getAllValues()
            );

            if (Tools::isSubmit('submitLogin')) {
                if ($login_form->submit()) {
                    $should_redirect = true;
                }
            }

            $this->context->smarty->assign([
                'login_form' => $login_form->getProxy(),
            ]);
            $this->setTemplate('customer/authentication');
        }

        parent::initContent();

        if ($should_redirect && !$this->ajax) {
            $back = urldecode(Tools::getValue('back'));

            if (Tools::urlBelongsToShop($back)) {
                // Checks to see if "back" is a fully qualified
                // URL that is on OUR domain, with the right protocol
                return $this->redirectWithNotifications($back);
            }

            // Well we're not redirecting to a URL,
            // so...
            if ($this->authRedirection) {
                // We may need to go there if defined
                return $this->redirectWithNotifications($this->authRedirection);
            }

            // go home
            return $this->redirectWithNotifications(__PS_BASE_URI__);
        }
    }
}

Link to comment
Share on other sites

  • 2 months later...
On 8/12/2018 at 3:31 PM, rygar said:

I managed to add the country field through smarty 

Make sure to change in the AuthController.php  to  $address->id_country = Tools::getValue('id_country');

 


        {if $field.name == 'id_country'}
          {$field.type = 'countrySelect'}
          {assign var='countries' value=Country::getCountries((int)$language.id, true)}
          {foreach from=$countries key=k item=v}
            {$field.availableValues.$k = $v.name}
          {/foreach}
        {/if}

 

 

Hello,

how can I add also the state of the coutry in the registration form?

 

thanks

Angela

 

 

Link to comment
Share on other sites

  • 2 months later...
On 9/18/2019 at 2:33 PM, joanna said:

Hi i'm having a problem with this code. I managed to make the address fields appear in the form but i can't get it to save them. I get a 500 error.  Could you please see if i'm doing something wrong?

Here is my AuthController.php override

 

class AuthController extends AuthControllerCore
{
    public $ssl = true;
    public $php_self = 'authentication';
    public $auth = false;

    public function checkAccess()
    {
        if ($this->context->customer->isLogged() && !$this->ajax) {
            $this->redirect_after = ($this->authRedirection) ? urlencode($this->authRedirection) : 'my-account';
            $this->redirect();
        }

        return parent::checkAccess();
    }

    public function initContent()
    {
        $should_redirect = false;

        if (Tools::isSubmit('submitCreate') || Tools::isSubmit('create_account')) {
            $register_form = $this
                ->makeCustomerForm()
                ->setGuestAllowed(false)
                ->fillWith(Tools::getAllValues());

            if (Tools::isSubmit('submitCreate')) {
                $hookResult = array_reduce(
                    Hook::exec('actionSubmitAccountBefore', array(), null, true),
                    function ($carry, $item) {
                        return $carry && $item;
                    },
                    true
                );
                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());
                    }
                }
                    
                
                {
                    
                    $should_redirect = true;
                }
            }
            

            $this->context->smarty->assign([
                'register_form' => $register_form->getProxy(),
                'hook_create_account_top' => Hook::exec('displayCustomerAccountFormTop'),
            ]);
            $this->setTemplate('customer/registration');
        } else {
            $login_form = $this->makeLoginForm()->fillWith(
                Tools::getAllValues()
            );

            if (Tools::isSubmit('submitLogin')) {
                if ($login_form->submit()) {
                    $should_redirect = true;
                }
            }

            $this->context->smarty->assign([
                'login_form' => $login_form->getProxy(),
            ]);
            $this->setTemplate('customer/authentication');
        }

        parent::initContent();

        if ($should_redirect && !$this->ajax) {
            $back = urldecode(Tools::getValue('back'));

            if (Tools::urlBelongsToShop($back)) {
                // Checks to see if "back" is a fully qualified
                // URL that is on OUR domain, with the right protocol
                return $this->redirectWithNotifications($back);
            }

            // Well we're not redirecting to a URL,
            // so...
            if ($this->authRedirection) {
                // We may need to go there if defined
                return $this->redirectWithNotifications($this->authRedirection);
            }

            // go home
            return $this->redirectWithNotifications(__PS_BASE_URI__);
        }
    }
}

same here. Did somone managed to achieve saving fields?

Link to comment
Share on other sites

  • 2 weeks later...

CURRENT SOLUTION

It seems like the initContent() function is NOT being executed when submitting the registration form in Prestashop 1.7.6.3... (perhaps above solutions worked in earlier 1.7.x versions?)

Therefore, I was NOT able to apply the above address save logic within the AuthController.php file...

HOWEVER - instead of adding to AuthController.php --- I'm able to update the ps_address file by updating override/classes/form/CustomerPersister.php
(Clear cache after updating below code)

1) Within CustomerPersister.php -- find the 2nd occurrence of 

$ok = $customer->save();

2) Directly below -- Add your Address Saving Code (slight variation of above solutions to allow same code to work within this logical position)..

         //address saving                
                    $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->vat_number = Tools::getValue('vat_number');
                    
                    $address->firstname = $customer->firstname;
                    $address->lastname = $customer->lastname;
                    $address->id_customer = (int) $customer->id;
                    
                    $address->id_state = 0;
                    $address->alias = 'My Address';                    
                    
                    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());
                    }

Edited by Marc Bastarache (see edit history)
Link to comment
Share on other sites

  • 5 weeks later...
  • 4 weeks later...
On 3/2/2020 at 4:18 PM, Marc Bastarache said:

CURRENT SOLUTION

It seems like the initContent() function is NOT being executed when submitting the registration form in Prestashop 1.7.6.3... (perhaps above solutions worked in earlier 1.7.x versions?)

Therefore, I was NOT able to apply the above address save logic within the AuthController.php file...

HOWEVER - instead of adding to AuthController.php --- I'm able to update the ps_address file by updating override/classes/form/CustomerPersister.php
(Clear cache after updating below code)

1) Within CustomerPersister.php -- find the 2nd occurrence of 

$ok = $customer->save();

2) Directly below -- Add your Address Saving Code (slight variation of above solutions to allow same code to work within this logical position)..

         //address saving                
                    $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->vat_number = Tools::getValue('vat_number');
                    
                    $address->firstname = $customer->firstname;
                    $address->lastname = $customer->lastname;
                    $address->id_customer = (int) $customer->id;
                    
                    $address->id_state = 0;
                    $address->alias = 'My Address';                    
                    
                    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());
                    }

I managed to carry out all these tips above. however for me it is extremely necessary that you can save the "state" information. This is not happening. how can I do so that this information is saved? already tried several ways and ends up in error 500

Prestashop 1.7.6.1

Link to comment
Share on other sites

  • 2 weeks later...
On 1/4/2020 at 7:27 PM, remyyyyy dice:

Bonjour,

Je ne recommande pas la solution de @Marc Bastarache mais plutôt d'utiliser le hook "actionObjectCustomerUpdateAfter" dans un module personnalisé 😎

Testé en version Prestashop 1.7.4 avec succés.

Cdt

seriez-vous assez aimable pour me dire étape par étape comment le faire?

Link to comment
Share on other sites

Il y a 18 heures, erserpico a dit :

seriez-vous assez aimable pour me dire étape par étape comment le faire?

Bonjour,

Pardonnez-moi mais pour le moment je suis over-booké.

Peut-être ultérieurement..

Cdt

Rémy

Link to comment
Share on other sites

On 3/2/2020 at 9:18 PM, Marc Bastarache said:

CURRENT SOLUTION

It seems like the initContent() function is NOT being executed when submitting the registration form in Prestashop 1.7.6.3... (perhaps above solutions worked in earlier 1.7.x versions?)

Therefore, I was NOT able to apply the above address save logic within the AuthController.php file...

HOWEVER - instead of adding to AuthController.php --- I'm able to update the ps_address file by updating override/classes/form/CustomerPersister.php
(Clear cache after updating below code)

1) Within CustomerPersister.php -- find the 2nd occurrence of 

$ok = $customer->save();

2) Directly below -- Add your Address Saving Code (slight variation of above solutions to allow same code to work within this logical position)..

         //address saving                
                    $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->vat_number = Tools::getValue('vat_number');
                    
                    $address->firstname = $customer->firstname;
                    $address->lastname = $customer->lastname;
                    $address->id_customer = (int) $customer->id;
                    
                    $address->id_state = 0;
                    $address->alias = 'My Address';                    
                    
                    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());
                    }

how can i apply this solution to saving new address on identity page? When, for example, client updating his phone and name in my account, in identity section, not in address section

 

Or perhaps how not to create new address, but update already existing address for same user

Edited by Myles_UA (see edit history)
Link to comment
Share on other sites

  • 6 months later...

I still have trouble adding “country” (and state) to the registration. I have seen the post about using something called “smarty” but don’t really understand it. Could someone maybe explain where I need to add what code. Would be really helpful. Thanx

Link to comment
Share on other sites

  • 3 months later...
Le 06/05/2020 à 7:47 PM, erserpico a dit :

seriez-vous assez aimable pour me dire étape par étape comment le faire?

Bonjour,

Chose promise, chose due, après tout ce temps, je prends "enfin" le temps de vous mettre un exemple :

public function hookActionObjectCustomerAddAfter($params) {

if (
        $this->context->controller->php_self == 'authentication'
        || $this->context->controller instanceof AdminCustomersController
    ) {
        if(!empty($params['object']->mon_champ_perso))
        {
            $this->updateCustomerMonChampPerso($params);
        }
    }

}

Au lieu de surcharger AuthController, le hookActionObjectCustomerAddAfter permet de mettre à jour le champ personnalisé par le client ou l'administrateur directement à partir de notre module..

Bien cordialement

Edited by remyyyyy (see edit history)
Link to comment
Share on other sites

  • 3 months later...
  • 8 months later...
On 3/2/2020 at 8:18 PM, Marc Bastarache said:

CURRENT SOLUTION

It seems like the initContent() function is NOT being executed when submitting the registration form in Prestashop 1.7.6.3... (perhaps above solutions worked in earlier 1.7.x versions?)

Therefore, I was NOT able to apply the above address save logic within the AuthController.php file...

HOWEVER - instead of adding to AuthController.php --- I'm able to update the ps_address file by updating override/classes/form/CustomerPersister.php
(Clear cache after updating below code)

1) Within CustomerPersister.php -- find the 2nd occurrence of 

$ok = $customer->save();

2) Directly below -- Add your Address Saving Code (slight variation of above solutions to allow same code to work within this logical position)..

         //address saving                
                    $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->vat_number = Tools::getValue('vat_number');
                    
                    $address->firstname = $customer->firstname;
                    $address->lastname = $customer->lastname;
                    $address->id_customer = (int) $customer->id;
                    
                    $address->id_state = 0;
                    $address->alias = 'My Address';                    
                    
                    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());
                    }


Add code inside CustomerPersister.php is good solution.




But if you want add County field, add this code 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;
        }



and this code in file override/classes/form/CustomerPersister.php:
 

//$address->id_country = (int) Tools::getCountry();
					
$address->id_country = (int)Tools::getValue('id_country') > 0 ? Tools::getValue('id_country') : (int) Tools::getCountry();



 

  • Like 1
Link to comment
Share on other sites

  • 6 months later...

This was my solution:

Thinking about how to solve the problem: forcing customer to register his address in customer registration process, I thought of something that is much easier, just redirect customer to the address register just after customer registration.

The solution was using the actionCustomerAccountAdd hook, in CustomerPersister.php

 

if ($ok) {
            $this->context->updateCustomer($customer);
            $this->context->cart->update();
            $this->sendConfirmationMail($customer);
            Hook::exec('actionCustomerAccountAdd', [
                'newCustomer' => $customer,
                Tools::redirect('index.php?controller=address')  //Redirect after registration
            ]);
        }

Regards

Edited by Welele (see edit history)
  • Like 3
Link to comment
Share on other sites

  • 4 months later...
  • 6 months later...
On 9/9/2022 at 9:33 AM, Welele said:

This was my solution:

Thinking about how to solve the problem: forcing customer to register his address in customer registration process, I thought of something that is much easier, just redirect customer to the address register just after customer registration.

The solution was using the actionCustomerAccountAdd hook, in CustomerPersister.php

 

if ($ok) {
            $this->context->updateCustomer($customer);
            $this->context->cart->update();
            $this->sendConfirmationMail($customer);
            Hook::exec('actionCustomerAccountAdd', [
                'newCustomer' => $customer,
                Tools::redirect('index.php?controller=address')  //Redirect after registration
            ]);
        }

Regards

I tried this and got an immediate Error 500 when tried to open an account. Could you give me an idea as to what went wrong please?

For reference, I added this line-   Tools::redirect('index.php?controller=address') //Redirect after registration    -in the appropriate place as shown.

Edited by Track (see edit history)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...