magento使用代码创建订单

首先感谢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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值