在本篇文章我将向你展示如何在Magento中以编程的方式创建订单
以下是我们在以下代码中遵循的步骤:
- 在Magento根文件夹中创建新的PHP文件,即create_custom_order.php
- 从app文件夹中包含Mage.php,其中包含所有Magento类。
- 初始化销售报价对象。
- 设置客户信息(名字,姓氏,电子邮件和地址),如果未注册,则创建新的客户帐户。
- 指定客户报价。
- 添加产品报价。
- 在报价上设置送货方式(统一费率)和付款方式(支票/汇票)。
- 最后,从报价创建订单。
以下是在Magento中以编程方式创建新订单的完整源代码:
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 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 | <?php require_once 'app/Mage.php'; umask(0); Mage::app('default'); $store = Mage::app()->getStore(); $website = Mage::app()->getWebsite(); // initialize sales quote object $quote = Mage::getModel('sales/quote')->setStoreId($storeId); // set customer information $customer_email = "donald.langer@email.com"; $customer_firstname = "Donald"; $customer_lastname = "Langer"; $billingAddress = array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => $customer_firstname, 'middlename' => '', 'lastname' => $customer_lastname, 'suffix' => '', 'company' => '', 'street' => array( '0' => 'Thunder River Boulevard', // required '1' => 'Customer Address 2' // optional ), 'city' => 'Teramuggus', 'country_id' => 'US', // country code 'region' => 'Alaska', 'region_id' => '2', 'postcode' => '99767', 'telephone' => '123-456-7890', 'fax' => '', 'save_in_address_book' => 1 ); $shippingAddress = array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => $customer_firstname, 'middlename' => '', 'lastname' => $customer_lastname, 'suffix' => '', 'company' => '', 'street' => array( '0' => 'Thunder River Boulevard', // required '1' => 'Customer Address 2' // optional ), 'city' => 'Teramuggus', 'country_id' => 'US', 'region' => 'Alaska', 'region_id' => '2', 'postcode' => '99767', 'telephone' => '123-456-7890', 'fax' => '', 'save_in_address_book' => 1 ); // check whether the customer already registered or not $customer = Mage::getModel('customer/customer')->setWebsiteId($website->getId())->loadByEmail($customer_email); if (!$customer->getId()) { // create the new customer account if not registered $customer = Mage::getModel('customer/customer'); $customer->setWebsiteId($website->getId()) ->setStore($store) ->setFirstname($customer_firstname) ->setLastname($customer_lastname) ->setEmail($customer_email); try { $password = $customer->generatePassword(); $customer->setPassword($password); // set the customer as confirmed $customer->setForceConfirmed(true); $customer->save(); $customer->setConfirmation(null); $customer->save(); // set customer address $customerId = $customer->getId(); $customAddress = Mage::getModel('customer/address'); $customAddress->setData($billingAddress) ->setCustomerId($customerId) ->setIsDefaultBilling('1') ->setIsDefaultShipping('1') ->setSaveInAddressBook('1'); // save customer address $customAddress->save(); // send new account email to customer $storeId = $customer->getSendemailStoreId(); $customer->sendNewAccountEmail('registered', '', $storeId); // set password remainder email if the password is auto generated by magento $customer->sendPasswordReminderEmail(); } catch (Exception $e) { Mage::logException($e); } } // assign the customer to quote $quote->assignCustomer($customer); // set currency for the quote $quote->setCurrency(Mage::app()->getStore()->getBaseCurrencyCode()); $productIds = array(337 => 2, 338 => 3); // add products to quote foreach($productIds as $productId => $qty) { $product = Mage::getModel('catalog/product')->load($productId); $quote->addProduct($product, $qty); } // add billing address to quote $billingAddressData = $quote->getBillingAddress()->addData($billingAddress); // add shipping address to quote $shippingAddressData = $quote->getShippingAddress()->addData($shippingAddress); // collect shipping rates on quote $shippingAddressData->setCollectShippingRates(true) ->collectShippingRates(); // set shipping method and payment method on the quote $shippingAddressData->setShippingMethod('flatrate_flatrate') ->setPaymentMethod('checkmo'); // Set payment method for the quote $quote->getPayment()->importData(array('method' => 'checkmo')); try { // collect totals & save quote $quote->collectTotals()->save(); // create order from quote $service = Mage::getModel('sales/service_quote', $quote); $service->submitAll(); $increment_id = $service->getOrder()->getRealOrderId(); echo 'Order Id: ' .$increment_id; } catch (Exception $e) { Mage::logException($e); } |
注意:此代码已在Magento 1.9.X中针对简单产品进行了测试。
希望这可以帮助你。