
MAGENTO 1.X – CREATE A SALES ORDER PROGRAMATICALLY
If you need to create a sales order programatically, the following code will help you:
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 |
public function createSalesOrderProgramattically($storeCode, $productItems, $customerData, $shippingData, $billingData){ $store = Mage::getModel('core/store')->load($storeCode, 'code'); $storeID = $store->getId(); $websiteId = $store->getWebsiteId(); // Start New Sales Order Quote $quote = Mage::getModel('sales/quote')->setStoreId($storeID); // Set Sales Order Quote Currency $quote->setCurrency(Mage::app()->getStore($storeID)->getCurrentCurrencyCode()); // Get customer $customer = Mage::getModel('customer/customer') ->setWebsiteId($websiteId) ->loadByEmail($customerData['email']); // Create customer in case that it not exists if (empty($customer->getData())) { $customer = Mage::getModel('customer/customer'); $customer->setWebsiteId($websiteId) ->setStore($store) ->setFirstname($customerData['firstname']) ->setLastname($customerData['lastname']) ->setEmail($customerData['email']) ->setPassword($customerData['password']); $customer->save(); } // Assign Customer To Sales Order Quote $quote->assignCustomer($customer); // Configure Notification $quote->setSendCconfirmation(1); foreach($productItems as $productItem){ $product = Mage::helper('catalog/product')->getProduct($productItem['sku'], $storeID, "sku" ); // You can replace the above line, with the procuct id. // $product = Mage::helper('catalog/product')->getProduct($productItem['id'], $storeID, "id" ); $quote->addProduct($product, new Varien_Object(array('qty' => $productItem['qty']))); } // Set Sales Order Billing Address $quote->getBillingAddress()->addData(array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => billingData['firstname'], 'middlename' => billingData['middlename'], 'lastname' => billingData['lastname'], 'suffix' => '', 'company' =>'', 'street' => array( '0' => billingData['street0'], '1' => billingData['street1'] ), 'city' => billingData['city'], 'country_id' => billingData['contry_id'], 'region' => billingData['region'], 'postcode' => billingData['postcode'], 'telephone' => billingData['telephone'], 'fax' => billingData['fax'], 'vat_id' => '', 'save_in_address_book' => 1 )); // Set Sales Order Shipping Address $shippingAddress = $quote->getShippingAddress()->addData(array( 'customer_address_id' => '', 'prefix' => '', 'firstname' => shippingData['firstname'], 'middlename' => shippingData['middlename'], 'lastname' =>shippingData['lastname'], 'suffix' => '', 'company' =>'', 'street' => array( '0' => shippingData['street0'], '1' => shippingData['street1'] ), 'city' => shippingData['city'], 'country_id' => shippingData['contry_id'], 'region' => shippingData['region'], 'postcode' => shippingData['postcode'], 'telephone' => shippingData['telephone'], 'fax' => shippingData['fax'], 'vat_id' => '', 'save_in_address_book' => 1 )); // Collect Rates and Set Shipping & Payment Method $shippingAddress->setCollectShippingRates(true) ->collectShippingRates() ->setShippingMethod('freeshipping_freeshipping'); // Set payment method $quote->getPayment()->setMethod('checkmo'); // 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(); // Dispatch corresponding events Mage::dispatchEvent( 'wyomind_advancedinventory_paypal_order_place_success', array('order' => $service->getOrder()) ); Mage::dispatchEvent('sales_order_place_after', array('order' => $service->getOrder())); // Resource Clean-Up $quote = $customer = $service = null; // Finished return $increment_id; } |