gpt4 book ai didi

以编程方式创建客户时,Magento 不会分配网站 ID

转载 作者:行者123 更新时间:2023-12-02 16:43:01 24 4
gpt4 key购买 nike

我正在以编程方式创建客户,但客户创建成功,但无法使用前端登录。问题是网站 ID 未分配给我尝试过以下代码的客户

if ($detail->ContactEmail && $detail->ContactName != '' && filter_var($detail->ContactEmail, FILTER_VALIDATE_EMAIL)) {
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(1);
$customer->loadByEmail($detail->ContactEmail);
/*
* Check if the email exist on the system.
* If YES, it will not create a user account.
*/
if (!$customer->getId()) {
//setting data such as email, firstname, lastname, and password
$customer->setEmail($detail->ContactEmail);
$name = preg_split('/\s+/', trim($detail->ContactName));

if (count($name) == 1) {
$customer->setFirstname($name[0]);
$customer->setLastname($name[0]);
} else {
$customer->setFirstname($name[0]);
$customer->setLastname($name[1]);
}
//$customer->setWebsiteId(array(1));
$customer->setcontactJobTitle($detail->ContactJobTitle);
$customer->setcontactSeqNo($detail->ContactSeqNo);
$customer->setdebtorAccNo($detail->DebtorAccNo);
$customer->setdebtorApiKey($debtorAPI);
$customer->setStoreId(Mage::app()->getStore('default')->getId());
$customer->setPassword($customer->generatePassword($passwordLength));
}
try {
//the save the data and send the new account email.
$customer->save();
$customer->setConfirmation(null);
$customer->save();
$customer->sendNewAccountEmail();
$customerCount[] = $i;
//echo 'contact added';
}
catch (Exception $e) {
//echo 'contact not added';
}

最佳答案

我已经找到拯救客户的问题出在哪里了。每当我们通过 loadByEmail 函数加载客户时,我们必须设置网站 ID 来加载客户,否则客户保存会引发异常使用网站范围时必须指定客户网站 ID。因此,我在创建客户时进行了以下更改以设置网站 id。

if ($detail->ContactEmail && $detail->ContactName != '' && filter_var($detail->ContactEmail, FILTER_VALIDATE_EMAIL)) {
$customer = Mage::getModel('customer/customer')->setWebsiteId(1);
$customer->loadByEmail($detail->ContactEmail); /* changed line */
/*
* Check if the email exist on the system.
* If YES, it will not create a user account.
*/
if (!$customer->getId()) {

//setting data such as email, firstname, lastname, and password
$customer->setEmail($detail->ContactEmail);
$name = preg_split('/\s+/', trim($detail->ContactName));

if (count($name) == 1) {
$customer->setFirstname($name[0]);
$customer->setLastname($name[0]);
} else {
$customer->setFirstname($name[0]);
$customer->setLastname($name[1]);
}

$customer->setcontactJobTitle($detail->ContactJobTitle);
$customer->setcontactSeqNo($detail->ContactSeqNo);
$customer->setdebtorAccNo($detail->DebtorAccNo);
$customer->setdebtorApiKey($debtorAPI);
$customer->setStoreId(Mage::app()->getStore('default')->getId());
$customer->setPassword($customer->generatePassword($passwordLength));

}
try {
//the save the data and send the new account email.
$customer->save();
$customer->setConfirmation(null);
$customer->setWebsiteId(1); /* changed line */
$customer->save();
$customer->sendNewAccountEmail();
$customerCount[] = $i;
//echo 'contact added';
}
catch (Exception $e) {
//echo 'contact not added';
}

关于以编程方式创建客户时,Magento 不会分配网站 ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20418735/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com