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

Zah*_*bas 2 magento

我以编程方式创建客户但客户创建成功但无法使用frontend.issue登录是网站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';
                        }
Run Code Online (Sandbox Code Playgroud)

Zah*_*bas 5

我发现拯救顾客的问题在哪里.每当我们按loadByEmail功能加载客户时,我们必须设置网站ID以加载客户,否则客户保存提出异常Customer website ID must be specified when using the website scope.所以我在创建客户时进行了以下更改以设置网站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';
                        }
Run Code Online (Sandbox Code Playgroud)