daily pastebin goal
77%
SHARE
TWEET

Create Order Programmatically

a guest Oct 25th, 2010 10,270 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
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top