Guest User

Create Order Programmatically

a guest
Oct 25th, 2010
10,952
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.  
  3. require_once 'app/Mage.php';
  4.  
  5. Mage::app();
  6.  
  7. $quote = Mage::getModel('sales/quote')
  8.     ->setStoreId(Mage::app()->getStore('default')->getId());
  9.  
  10. if ('do customer orders') {
  11.     // for customer orders:
  12.     $customer = Mage::getModel('customer/customer')
  13.         ->setWebsiteId(1)
  14.         ->loadByEmail('customer@example.com');
  15.     $quote->assignCustomer($customer);
  16. } else {
  17.     // for guesr orders only:
  18.     $quote->setCustomerEmail('customer@example.com');
  19. }
  20.  
  21. // add product(s)
  22. $product = Mage::getModel('catalog/product')->load(8);
  23. $buyInfo = array(
  24.     'qty' => 1,
  25.     // custom option id => value id
  26.     // or
  27.     // configurable attribute id => value id
  28. );
  29. $quote->addProduct($product, new Varien_Object($buyInfo));
  30.  
  31. $addressData = array(
  32.     'firstname' => 'Test',
  33.     'lastname' => 'Test',
  34.     'street' => 'Sample Street 10',
  35.     'city' => 'Somewhere',
  36.     'postcode' => '123456',
  37.     'telephone' => '123456',
  38.     'country_id' => 'US',
  39.     'region_id' => 12, // id from directory_country_region table
  40. );
  41.  
  42. $billingAddress = $quote->getBillingAddress()->addData($addressData);
  43. $shippingAddress = $quote->getShippingAddress()->addData($addressData);
  44.  
  45. $shippingAddress->setCollectShippingRates(true)->collectShippingRates()
  46.         ->setShippingMethod('flatrate_flatrate')
  47.         ->setPaymentMethod('checkmo');
  48.  
  49. $quote->getPayment()->importData(array('method' => 'checkmo'));
  50.  
  51. $quote->collectTotals()->save();
  52.  
  53. $service = Mage::getModel('sales/service_quote', $quote);
  54. $service->submitAll();
  55. $order = $service->getOrder();
  56.  
  57. printf("Created order %s\n", $order->getIncrementId());
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×