4

当客户或用户注册时,我想一键捕获地址详细信息并将其保存在 ps_address 表中,即当他点击注册按钮时,他的地址详细信息也必须保存到 ps_address 表中,如何做到这一点?在此处输入图像描述

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

现在我坚持的是:当我点击注册按钮时,地址字段详细信息没有保存在数据库中,我收到服务器错误

我试图做的是,我创建了一个名为 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             }
    }

请帮助我或告诉我我是否做错了什么或如何做到这一点

4

1 回答 1

3

我通过添加解决了它$address->alias,因为别名是必需的并且已经过验证。另外为了保存在我修改 $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();
    }           
}
于 2016-03-17T10:10:28.380 回答