php – 在Prestashop中的Registeration表单中添加地址字段

当客户或用户注册时,我想一键捕获地址详细信息并将其保存在ps_address表中,即当他点击注册按钮时,他的地址详细信息也必须保存到ps_address表中,该怎么做?
《php – 在Prestashop中的Registeration表单中添加地址字段》

我已设法自定义注册表单,并能够将地址详细信息表单插入我的注册表单,如附图所示:

现在我所困扰的是:当点击“注册”按钮时,地址字段详细信息未保存在数据库中,我收到服务器错误

我试图做的是,我创建了一个名为processPostAddress的新函数,我在重定向到帐户页面之前从controller / front / Authcontroller.php页面中的processSubmitAccount()调用processPostAddress.

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

下面是我在controller / front / Authcontroller.php页面中创建的自定义函数

      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             }
    }

请帮助我或告诉我,如果我做错了什么或如何实现这一目标

最佳答案 我通过添加$address->别名来解决它,因为别名是必需的并且已经过验证.

另外为了保存在数据库中,我修改了$address-> add();到$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();
    }           
}
点赞