首先感谢inchoo的文章:Programmatically create order in Magento 但此文章的方法有点复杂。于是见其下面的评论, Vinai 出场了,见:http://pastebin.com/8cft4d8v 终于,我找到了想要的代码。
代码分享如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
<?php
require_once
'app/Mage.php'
;
Mage::app();
$quote
= Mage::getModel(
'sales/quote'
)
->setStoreId(Mage::app()->getStore(
'default'
)->getId());
if
(1) {
// for customer orders:
$customer
= Mage::getModel(
'customer/customer'
)
->setWebsiteId(1)
->loadByEmail(
'493835927@qq.com'
);
$quote
->assignCustomer(
$customer
);
}
else
{
// for guesr orders only:
$quote
->setCustomerEmail(
'customer@example.com'
);
}
// add product(s)
$product
= Mage::getModel(
'catalog/product'
)->load(166);
$buyInfo
=
array
(
'qty'
=> 1,
// custom option id => value id
// or
// configurable attribute id => value id
);
$quote
->addProduct(
$product
,
new
Varien_Object(
$buyInfo
));
$addressData
=
array
(
'firstname'
=>
'Test'
,
'lastname'
=>
'Test'
,
'street'
=>
'Sample Street 10'
,
'city'
=>
'Somewhere'
,
'postcode'
=>
'123456'
,
'telephone'
=>
'123456'
,
'country_id'
=>
'US'
,
'region_id'
=> 12,
// id from directory_country_region table
);
$billingAddress
=
$quote
->getBillingAddress()->addData(
$addressData
);
$shippingAddress
=
$quote
->getShippingAddress()->addData(
$addressData
);
$shippingAddress
->setCollectShippingRates(true)->collectShippingRates()
->setShippingMethod(
'flatrate_flatrate'
)
->setPaymentMethod(
'checkmo'
);
$quote
->getPayment()->importData(
array
(
'method'
=>
'checkmo'
));
$quote
->collectTotals()->save();
$service
= Mage::getModel(
'sales/service_quote'
,
$quote
);
$service
->submitAll();
$order
=
$service
->getOrder();
printf(
"Created order %s\n"
,
$order
->getIncrementId());
|
如上 为我自己做测试时使用的代码,设置上了必要的参数。请按照你的magento环境设置相应的参数。详情见我开始提到的原代码出处。
转载标明出处:www.hellokeykey.com