如何在prestashop中更新来自其他表单的客户电子邮件

时间:2016-06-13 10:24:24

标签: prestashop prestashop-1.6 prestashop-1.7

我正在验证手机号码并检索客户的电子邮件地址,并允许客户更改该表单中的电子邮件地址,现在我需要更新电子邮件地址,如何从我的自定义控制器更新,即如何调用Customer类和在prestashop中更新我的电子邮件地址

elseif (Tools::isSubmit('update_email')) {
    $otp_mobile_num = trim(Tools::getValue('otp_mobile_num'));
    $update_email = Tools::getValue('update_email_address');
    $old_email = Tools::getValue('old_email_address');  
    //d($otp_mobile_num);
    if($old_email!= $update_email) {
        //d($update_email);
        // Checked the email address is already in use, in case he changed his email address
        if (Validate::isEmail($update_email) && !empty($update_email)) {
            if (Customer::customerExists($update_email)) {
                $this->errors[] = Tools::displayError('An account using this email address :('.$update_email.') has already been registered.', false);
            }
        }
    } else {
         $this->errors[] = Tools::displayError('You must change the email address!');
    }
}

以下是我的单独表格:

enter image description here

1 个答案:

答案 0 :(得分:0)

您可以尝试直接更新上下文客户:

elseif (Tools::isSubmit('update_email')) {
    $otp_mobile_num = trim(Tools::getValue('otp_mobile_num'));
    $update_email = Tools::getValue('update_email_address');
    $old_email = Tools::getValue('old_email_address');  
    //d($otp_mobile_num);
    if($old_email!= $update_email){
        //d($update_email);
        // Checked the email address is already in use, in case he changed his email address
        if (Validate::isEmail($update_email) && !empty($update_email)) {
            if (Customer::customerExists($update_email)) {
                $this->errors[] = Tools::displayError('An account using this email address :('.$update_email.') has already been registered.', false);
            } elseif(Context::getContext()->customer->id == null) {
                $this->errors[] = Tools::displayError('You must be logged in');
            } else {
                Context::getContext()->customer->email = $update_email
                if(Context::getContext()->customer->save()) {
                    // Customer updated correctly
                } else {
                    $this->errors[] = Tools::displayError('An error occured');
                }
            }
        }
    }else{
         $this->errors[] = Tools::displayError('You must change the email address!');
    }
}