1.首先创建一个用户,并创建改用户登录的session
$customer = Mage::getModel('customer/customer');//$customer = new Mage_Customer_Model_Customer(); $password = '123456';$email = 'ajzele@mymail.com'; $customer->setWebsiteId(Mage::app()->getWebsite()->getId());$customer->loadByEmail($email);//Zend_Debug::dump($customer->debug()); exit; if(!$customer->getId()) { $customer->setEmail($email);$customer->setFirstname('Johnny');$customer->setLastname('Doels');$customer->setPassword($password);} try {$customer->save();$customer->setConfirmation(null);$customer->save(); //Make a "login" of new customerMage::getSingleton('customer/session')->loginById($customer->getId());} catch (Exception $ex) {//Zend_Debug::dump($ex->getMessage());}
这段代码可运行在magento的任何地方
这样我们就创建了一个新的用户,并让他是登陆的状态
现在给这个用户添加shipping和billing address,记住这是订单所必须的,这里的情况是使用同样的地址于shipping和billing address
你也可以创建不同的地址,给不同的默认地址
//Build billing and shipping address for customer, for checkout$_custom_address = array ('firstname' => 'Branko','lastname' => 'Ajzele','street' => array ('0' => 'Sample address part1','1' => 'Sample address part2',), 'city' => 'Osijek','region_id' => '','region' => '','postcode' => '31000','country_id' => 'HR', /* Croatia */'telephone' => '0038531555444',); $customAddress = Mage::getModel('customer/address')//$customAddress = new Mage_Customer_Model_Address();$customAddress->setData($_custom_address)->setCustomerId($customer->getId())->setIsDefaultBilling('1')->setIsDefaultShipping('1')->setSaveInAddressBook('1'); try {$customAddress->save();}catch (Exception $ex) {//Zend_Debug::dump($ex->getMessage());} Mage::getSingleton('checkout/session')->getQuote()->setBillingAddress(Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress));Mage::getSingleton('checkout/session')->getQuote()->setshippingAddress(Mage::getSingleton('sales/quote_address')->importCustomerAddress($customAddress));
//这两句要单独理解,按流程来看,先是给添加产品到购物车才会产生quote,接着到checkout页面,保存地址到quote
来给购物车添加产品
/* If we wish to load some product by some attribute value diferent then id */$product = Mage::getModel('catalog/product')->getCollection()/* Remember, you can load/find product via any attribute, better if its attribute with unique value */->addAttributeToFilter('sku', 'some-sku-value')->addAttributeToSelect('*')->getFirstItem(); /* Do a full product load, otherwise you might get some errors related to stock item */$product->load($product->getId()); $cart = Mage::getSingleton('checkout/cart'); /* We want to add only the product/products for this user and do so programmatically, so lets clear cart before we start adding the products into it */$cart->truncate();$cart->save();$cart->getItems()->clear()->save(); try {/* Add product with custom oprion? => some-custom-option-id-here: value to be read from database or assigned manually, hardcoded? Just example*///$cart->addProduct($product, array('options'=> array('some-custom-option-id-here' => 'Some value goes here');$cart->save();}catch (Exception $ex) {echo $ex->getMessage();} unset($product);
最后创建保存订单
$storeId = Mage::app()->getStore()->getId(); $checkout = Mage::getSingleton('checkout/type_onepage'); $checkout->initCheckout(); $checkout->saveCheckoutMethod('register');//需要单独理解 $checkout->saveShippingMethod('flatrate_flatrate'); $checkout->savePayment(array('method'=>'checkmo')); try {$checkout->saveOrder();}catch (Exception $ex) {//echo $ex->getMessage();} /* Clear the cart */ $cart->truncate();$cart->save();$cart->getItems()->clear()->save(); /* Logout the customer you created */Mage::getSingleton('customer/session')->logout();
这些代码基于magento 1.3老版本了,最新的版本需要测试是否可用,主要的作用是帮助理解流程,有些片段代码还是很有用的,比如添加修改客户的默认地址
转载于http://inchoo.net/magento/programming-magento/programatically-create-customer-and-order-in-magento-with-full-blown-one-page-checkout-process-under-the-hood/
部分翻译,部分自写