Magento 创建用户代码 Creating a new customer in Magento(RESTful,PHP)

$customer = Mage::getModel('customer/customer');
//$customer  = new Mage_Customer_Model_Customer();
 
$password = 'iks123456';
$email = 'gotodiscuss@ikeepstudying.com';
 
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($email);
//Zend_Debug::dump($customer->debug()); exit;
 
if(!$customer->getId()) 
{
    $customer->setEmail($email);
    $customer->setFirstname('ikeepstudying');
    $customer->setLastname('Liang');
    $customer->setPassword($password);
}
 
try 
{
    $customer->save();
    $customer->setConfirmation(null);
    $customer->save();
    $customer->sendNewAccountEmail();
 
    //Make a "login" of new customer
    Mage::getSingleton('customer/session')->loginById($customer->getId());
}
 
catch (Exception $ex) {
    //Zend_Debug::dump($ex->getMessage());
}   


$user = Mage::getSingleton('customer/session')->getCustomer();
echo '<pre>'; print_r($user); echo '</pre>';

更多:

$address = Mage::getModel("customer/address");
$address->setCustomerId($customer->getId())
        ->setFirstname($customer->getFirstname())
        ->setMiddleName($customer->getMiddlename())
        ->setLastname($customer->getLastname())
        ->setCountryId('HR')
		//->setRegionId('1') //state/province, only needed if the country is USA
        ->setPostcode('31000')
        ->setCity('Osijek')
        ->setTelephone('0038511223344')
        ->setFax('0038511223355')
        ->setCompany('Inchoo')
        ->setStreet('Kersov')
        ->setIsDefaultBilling('1')
        ->setIsDefaultShipping('1')
        ->setSaveInAddressBook('1');
 
try{
    $address->save();
}
catch (Exception $e) {
    Zend_Debug::dump($e->getMessage());
}

 

数据库操作参考: Magento 数据库查询速记

更多参考:

Programatically create customer and order in Magento with full blown One Page Checkout process under the hood

Programmatically adding new customers to the Magento store

原文/本文:Magento 创建用户代码 Creating a new customer in Magento(RESTful,PHP)

Leave a Reply