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

时间:2013-12-06 07:48:27

标签: 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';
                        }

2 个答案:

答案 0 :(得分: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';
                        }

答案 1 :(得分:2)

请找到以下代码以编程方式创建客户:

    error_reporting(E_ALL | E_STRICT);
    $mageFilename = 'app/Mage.php';

    if (!file_exists($mageFilename)) {
        if (is_dir('downloader')) {
        header("Location: downloader");
        } else {
        echo $mageFilename." was not found";
        }
        exit;
    }

    require_once $mageFilename;
    Varien_Profiler::enable();
    Mage::setIsDeveloperMode(true);
    ini_set('display_errors', 1);
    umask(0);

    Mage::app('default');

    $customer_email = 'test@testemail.com';  
    $customer_fname = 'test_firstname';      
    $customer_lname = 'test_lastname';    
    $passwordLength = 10;       

    $customer = Mage::getModel('customer/customer');
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId());
    $customer->loadByEmail($customer_email);



    if(!$customer->getId()) {



      $customer->setEmail($customer_email); 
      $customer->setFirstname($customer_fname);
      $customer->setLastname($customer_lname);
      $customer->setPassword($customer->generatePassword($passwordLength));

    }
    try{

          $customer->save();
          $customer->setConfirmation(null);
          $customer->save(); 
      }

=============================================== =================================