1.首先建立一个用户,并建立改用户登陆的sessionphp
$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 customer Mage::getSingleton('customer/session')->loginById($customer->getId()); } catch (Exception $ex) { //Zend_Debug::dump($ex->getMessage()); }
这段代码可运行在magento的任何地方session
这样咱们就建立了一个新的用户,并让他是登录的状态app
如今给这个用户添加shipping和billing address,记住这是订单所必须的,这里的状况是使用一样的地址于shipping和billing addresspost
你也能够建立不一样的地址,给不一样的默认地址测试
//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页面,保存地址到quoteui
来给购物车添加产品this
/* 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);
最后建立保存订单.net
$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/debug
部分翻译,部分自写