当客户或用户注册时,我想一键捕获地址详细信息并将其保存在 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 }
}
如果我做错了什么或如何实现这一目标,请帮助我或告诉我
最佳答案
我通过添加 $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();
}
}
关于php - 在 Prestashop 的注册表中添加地址字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36054040/