magento 經過程序建立客戶和訂單

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

部分翻譯,部分自寫

相關文章
相關標籤/搜索