Advertisement
Guest User

Untitled

a guest
Oct 31st, 2010
258
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.01 KB | None | 0 0
  1. <?
  2. $this->_initStore();
  3.  
  4. $customer = null; // get customer object here
  5.  
  6. // initialise quote object
  7. $quote = Mage::getModel('sales/quote');
  8.  
  9. // set Customer
  10. $quote->assignCustomer($customer);
  11.  
  12. // add product to quote object
  13. $request =  array(
  14.         'qty' => 1, // qty ordered
  15.         'product' => $product_id,
  16.         //'super_attribute' => array( // this is optinal
  17.             //  '631' => '13',
  18.             //  '632' => '14',
  19.         //),
  20. );
  21.  
  22. $product = Mage::getModel('catalog/product')->load($product_id);
  23.  
  24. $quote->addProduct($product, new Varien_Object($request));
  25.  
  26.  
  27. // set billing address
  28. $billingAddress = array(
  29.         'firstname' => 'John',
  30.         'lastname' => 'Doe',
  31.         'street' => 'street',
  32.         'city' => 'city',
  33.         'country_id' => 'country'
  34. );
  35.  
  36. $quoteBillingAddress = new Mage_Sales_Model_Quote_Address($billingAddress);
  37. $quote->setBillingAddress($quoteBillingAddress);
  38. $quote->setRemoteIp($customer_ip);
  39. $quote->setCustomerNote($boxaddress);
  40.  
  41. #        $quote->getBillingAddress();
  42. #        $quote->getShippingAddress()->setCollectShippingRates(true);
  43.  
  44. // collect totals and save
  45. $quote->collectTotals();        // calls $address->collectTotals();
  46.  
  47. $quote->save();
  48.  
  49. // reserver order id
  50. $quote->reserveOrderId();
  51.  
  52. // set payment method
  53. $quotePaymentObj = $quote->getPayment(); // Mage_Sales_Model_Quote_Payment
  54. $pamyentData = array(
  55.                                         'method' => 'purchaseorder',
  56.                                         'po_number' => $customer_id.'_'.$quote->getReservedOrderId(),
  57.                                 );
  58. $quotePaymentObj->importData($pamyentData);
  59. $quote->setPayment($quotePaymentObj);
  60.  
  61. // initialise objetct to convert quote data to order dta
  62. $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
  63.  
  64. // convert adress in quote object
  65. $order = $convertQuoteObj->addressToOrder($quote->getBillingAddress());
  66.  
  67. // convert payment in object
  68. $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);
  69.  
  70. // set billing adress to objectg
  71. $order->setBillingAddress($convertQuoteObj->addressToOrderAddress($quote->getBillingAddress()));
  72.  
  73. // set payment to object
  74. $order->setPayment($convertQuoteObj->paymentToOrderPayment($quote->getPayment()));
  75.  
  76.  
  77. // get all items and convert them to order items
  78. $items = $quote->getAllItems();
  79.    
  80.     foreach ($items as $item) {
  81.  
  82.             $orderItem = $convertQuoteObj->itemToOrderItem($item);
  83.             if ($item->getParentItem()) {
  84.                     $orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
  85.             }
  86.             $order->addItem($orderItem);
  87.     }        
  88.  
  89.    
  90.     try {
  91.             // place the order
  92.             $order->place()->save();
  93.  
  94.             $customerEmail = $order->getCustomerEmail();
  95.             $order->setCustomerEmail('dlight@flimmit.com');         # don't send order email to customer!!!
  96.             $order->sendNewOrderEmail();
  97.             $order->setCustomerEmail($customerEmail);
  98.  
  99.             $order->addStatusHistoryComment('Order created by API', Mage_Sales_Model_Order::STATE_PENDING_PAYMENT);
  100.             // save the order
  101.             $order->save();
  102.     } catch (Exception $e) {
  103.    
  104.     }
  105.    
  106.    
  107.     // exit for now
  108.     die('thats it, no invoice yet');// create invoice for order
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement