Magento1:以编程方式创建客户

本文介绍如何以编程方式(通过代码)在Magento 1.x中创建客户。

在此示例代码中,您可以找到保存客户的一般信息以及保存客户的帐单地址和送货地址。

以下是以下代码中遵循的步骤

 代码首先通过电子邮件加载客户。

  • 如果客户电子邮件已经存在(即客户已经注册),则它会检查客户是否已经登录。如果客户已经登录,则会将他/她跳转到客户仪表板页面。否则,它会将他/她跳转到客户登录页面。
  • 如果电子邮件未在系统中注册,则会创建新客户。之后,还会为新创建的客户保存客户地址。在创建客户时,您可以输入自定义密码或让Magento生成密码。
  • 通过电子邮件向客户发送有关其新帐户的创建信息。
  • 日志消息保存在日志文件中。

 
以下是以编程方式创建新客户的完整源代码:

$store = Mage::app()->getStore();
$website = Mage::app()->getWebsite();
$websiteId = $website->getId();
 
$firstName = 'John';
$lastName = 'Doe';
$email = 'johndoe4@example.com';
$logFileName = 'my-log-file.log';
 
$address = array(
            'customer_address_id' => '',
            'prefix' => '',
            'firstname' => $firstName,
            'middlename' => '',
            'lastname' => $lastName,
            'suffix' => '',
            'company' => '', 
            'street' => array(
                 '0' => 'Your Customer Address 1', // compulsory
                 '1' => 'Your Customer Address 2' // optional
             ),
            'city' => 'Culver City',
            'country_id' => 'US', // two letters country code
            'region' => 'California', // can be empty '' if no region
            'region_id' => '12', // can be empty '' if no region_id
            'postcode' => '90232',
            'telephone' => '888-888-8888',
            'fax' => '',
            'save_in_address_book' => 1
        );
 // check if the email address is already registered
$customer = Mage::getModel('customer/customer')
                ->setWebsiteId($websiteId)
                ->loadByEmail($email);
/** 
 * If email already registered, redirect to customer login page
 * Else create new customer 
 */ 
if ($customer->getId()) {        
    Mage::log('Customer with email '.$email.' is already registered.', null, $logFileName);
    
    if( !Mage::getSingleton('customer/session')->isLoggedIn() ) {             
        //Mage::getSingleton('customer/session')->setBeforeAuthUrl(Mage::getUrl('checkout/onepage')); 
        
        Mage::app()->getResponse()
                   ->setRedirect(Mage::getUrl('customer/account/login'))
                   ->sendResponse();
            
        //$this->_redirect('customer/account/login');        
    } else {         
        Mage::app()->getResponse()
                   ->setRedirect(Mage::getUrl('customer/account'))
                   ->sendResponse();
    }
        
} else {    
    $customer = Mage::getModel('customer/customer');
    
    $password = $customer->generatePassword(); // you can write your custom password here instead of magento generated password
    
    $customer->setWebsiteId($websiteId)
             ->setStore($store)
             ->setFirstname($firstName)
             ->setLastname($lastName)
             ->setEmail($email)
             ->setPassword($password);
                                
    try {
        // set the customer as confirmed
        $customer->setForceConfirmed(true);
        
        // save customer
        $customer->save();
        
        $customer->setConfirmation(null);
        $customer->save();
        
        // set customer address
        $customerId = $customer->getId();        
        $customAddress = Mage::getModel('customer/address');            
        $customAddress->setData($address)
                      ->setCustomerId($customerId)
                      ->setIsDefaultBilling('1')
                      ->setIsDefaultShipping('1')
                      ->setSaveInAddressBook('1');
        
        // save customer address
        $customAddress->save();
        
        // send new account email to customer    
        //$customer->sendNewAccountEmail();
        $storeId = $customer->getSendemailStoreId();
        $customer->sendNewAccountEmail('registered', '', $storeId);
        
        // set password remainder email if the password is auto generated by magento
        $customer->sendPasswordReminderEmail();
        
        // auto login customer
        //Mage::getSingleton('customer/session')->loginById($customer->getId());
        
        Mage::log('Customer with email '.$email.' is successfully created.', null, $logFileName);
        
        echo 'Customer with email '.$email.' is successfully created. 
'; } catch (Mage_Core_Exception $e) { if (Mage::getSingleton('customer/session')->getUseNotice(true)) { Mage::getSingleton('customer/session')->addNotice(Mage::helper('core')->escapeHtml($e->getMessage())); } else { $messages = array_unique(explode("\n", $e->getMessage())); foreach ($messages as $message) { Mage::getSingleton('customer/session')->addError(Mage::helper('core')->escapeHtml($message)); } } } catch (Exception $e) { //Zend_Debug::dump($e->getMessage()); Mage::getSingleton('customer/session')->addException($e, $this->__('Cannot add customer')); Mage::logException($e); //$this->_goBack(); } }

文章参考翻译:
http://blog.chapagain.com.np/magento-create-customer-programmatically/

Leave a comment

您的电子邮箱地址不会被公开。 必填项已用 * 标注