
首先感谢inchoo的文章:Programmatically create order in Magento 但此文章的方法有点复杂。于是见其下面的评论, Vinai 出场了,见:http://pastebin.com/8cft4d8v 终于,我找到了想要的代码。
代码分享如下
<?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