当客户或用户注册时,我想一键捕获地址详细信息并将其保存在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 }
}
如果我做错了什么或如何做到这一点,请帮助我或告诉我
答案 0 :(得分:2)
我通过添加$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();
}
}