The below script is used to pragmatically create an order in Magento. Most of the time I run this script to ensure that observers are called on order create. This save you having to create new orders in the admin or frontend of Magento which can be tedious and time consuming when debugging.
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 |
<?php require_once './app/Mage.php'; //path to Mage.php Mage::app(); $quote = Mage::getModel('sales/quote') ->setStoreId(Mage::app()->getStore('gb')->getId()); // Change to your store code $customerId = false; // set this to an existing customer ID if you wish. Otherwise continue as guest if ($customerId) { // for customer orders: $customer = Mage::getModel('customer/customer') ->setWebsiteId(1) ->loadByEmail('test@example.com'); // Change to your email $quote->assignCustomer($customer); } else { // for guesr orders only: $quote->setCustomerEmail('test@example.com'); // Change to your email } // add product(s) $product = Mage::getModel('catalog/product')->load(13120); // Change to a product id on your store $buyInfo = array( 'qty' => 1, ); $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()); |
Usage :
Name the file create_order.php and place in the Magento root of your install. You can visit in browser or run from your Command Line Interface.