Search code examples
phpsmartyprestashopprestashop-1.6prestashop-1.7

Adding Address Fields to the Registeration form in Prestashop


When a customer or user registers, I want to capture the address details in one click and save that in ps_address table , ie when he clicks on the register button his address details must be also saved to the ps_address table , how to do this?enter image description here

I have managed to customize the registration form and able to plug the address details form to my registration form as shown in the attached image :

Now what I am stuck with is : when am clicking on the Register button ,the address field details are not saved in the database and I am getting server error

What I tried to do is ,I created a new function called processPostAddress and I am calling processPostAddress from processSubmitAccount() in controller/front/Authcontroller.php page before the redirection to the account page.

 $this->processPostAddress(); //// custom function call 
 Tools::redirect('index.php?controller='.(($this->authRedirection !== false) ? urlencode($this->authRedirection) : 'my-account'));

Below is the custom function which i created in controller/front/Authcontroller.php page

      public function processPostAddress()
    {
        if($this->context->customer->id_customer!=''){
        $address                = new Address();
        $address->id_customer   = 40;        
        $address->firstname     = trim(Tools::getValue('firstname'));
        $address->lastname      = trim(Tools::getValue('lastname'));
        $address->address1      = trim(Tools::getValue('address1'));
        $address->address2      = trim(Tools::getValue('address2'));
        $address->postcode      = trim(Tools::getValue('postcode'));
        $address->city          = trim(Tools::getValue('city'));
        $address->country       = trim(Tools::getValue('country'));
        $address->state         = trim(Tools::getValue('state'));
        $address->phone         = trim(Tools::getValue('phone'));
        $address->phone_mobile  = trim(Tools::getValue('phone_mobile'));
        $address->add(); // This should add the address to the addresss table             }
    }

Please help me or tell me if I am doing anything wrong or how to achieve this


Solution

  • I solved it by adding $address->alias, since alias was required and was validated . Also in order to save in the database I modified $address->add(); to $address->save();

    public function processPostAddress()
    {       
        ///Address::postProcess(); // Try this for posting address  and check if its working
        // Preparing Address 
            $address                = new Address();
            $this->errors           = $address->validateController();
            $address->id_customer   = (int)$this->context->customer->id;        
            $address->firstname     = trim(Tools::getValue('firstname'));
            $address->lastname      = trim(Tools::getValue('lastname'));
            $address->address1      = trim(Tools::getValue('address1'));
            $address->address2      = trim(Tools::getValue('address2'));
            $address->postcode      = trim(Tools::getValue('postcode'));
            $address->city          = (int)trim(Tools::getValue('city'));
            $address->country       = (int)trim(Tools::getValue('country'));
            $address->state         = (int)trim(Tools::getValue('state'));
            $address->phone         = trim(Tools::getValue('phone'));
            $address->alias         = "My Default Address";
    
        // Check the requires fields which are settings in the BO
        $this->errors = array_merge($this->errors, $address->validateFieldsRequiredDatabase());
             // Don't continue this process if we have errors !
    
        if (!$this->errors && !$this->ajax) {
            return;
        }else{
             // Save address
            $address->save();
        }           
    }