title: Create order programmatically in Magento
body: <p>I'm using these two methods to create orders programmatically in Magento. </p>
  
  <p>The first one creates a Quote:</p>
  
  <pre><code> public function prepareCustomerOrder($customerId, array $shoppingCart, array  $shippingAddress, array $billingAddress, 
          $shippingMethod, $couponCode = null) 
      {
          $customerObj = Mage::getModel('customer/customer')-&gt;load($customerId);
          $storeId = $customerObj-&gt;getStoreId();
          $quoteObj = Mage::getModel('sales/quote')-&gt;assignCustomer($customerObj);
          $storeObj = $quoteObj-&gt;getStore()-&gt;load($storeId);
          $quoteObj-&gt;setStore($storeObj);
  
          // add products to quote
          foreach($shoppingCart as $part) {
              $productModel = Mage::getModel('catalog/product');
              $productObj = $productModel-&gt;setStore($storeId)-&gt;setStoreId($storeId)-&gt;load($part['PartId']);
  
              $productObj-&gt;setSkipCheckRequiredOption(true);
  
              try{
                  $quoteItem = $quoteObj-&gt;addProduct($productObj);
                  $quoteItem-&gt;setPrice(20);
                  $quoteItem-&gt;setQty(3);
                  $quoteItem-&gt;setQuote($quoteObj);                                    
                  $quoteObj-&gt;addItem($quoteItem);
  
              } catch (exception $e) {
              return false;
              }
  
              $productObj-&gt;unsSkipCheckRequiredOption();
              $quoteItem-&gt;checkData();
          }
  
          // addresses
          $quoteShippingAddress = new Mage_Sales_Model_Quote_Address();
          $quoteShippingAddress-&gt;setData($shippingAddress);
          $quoteBillingAddress = new Mage_Sales_Model_Quote_Address();
          $quoteBillingAddress-&gt;setData($billingAddress);
          $quoteObj-&gt;setShippingAddress($quoteShippingAddress);
          $quoteObj-&gt;setBillingAddress($quoteBillingAddress);
  
          // coupon code
          if(!empty($couponCode)) $quoteObj-&gt;setCouponCode($couponCode); 
  
  
          // shipping method an collect
          $quoteObj-&gt;getShippingAddress()-&gt;setShippingMethod($shippingMethod);
          $quoteObj-&gt;getShippingAddress()-&gt;setCollectShippingRates(true);
          $quoteObj-&gt;getShippingAddress()-&gt;collectShippingRates();
          $quoteObj-&gt;collectTotals(); // calls $address-&gt;collectTotals();
          $quoteObj-&gt;setIsActive(0);
          $quoteObj-&gt;save();
  
          return $quoteObj-&gt;getId();
  
      }
  </code></pre>
  
  <p>And the second one uses that Quote to create Order:</p>
  
  <pre><code> public function createOrder($quoteId, $paymentMethod, $paymentData) 
      {
          $quoteObj = Mage::getModel('sales/quote')-&gt;load($quoteId); // Mage_Sales_Model_Quote
          $items = $quoteObj-&gt;getAllItems();                  
  
          $quoteObj-&gt;reserveOrderId();
  
            // set payment method 
          $quotePaymentObj = $quoteObj-&gt;getPayment(); // Mage_Sales_Model_Quote_Payment
          $quotePaymentObj-&gt;setMethod($paymentMethod);
          $quoteObj-&gt;setPayment($quotePaymentObj);
  
          // convert quote to order
          $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
          $orderObj = $convertQuoteObj-&gt;addressToOrder($quoteObj-&gt;getShippingAddress());
          $orderPaymentObj = $convertQuoteObj-&gt;paymentToOrderPayment($quotePaymentObj);
  
          // convert quote addresses
          $orderObj-&gt;setBillingAddress($convertQuoteObj-&gt;addressToOrderAddress($quoteObj-&gt;getBillingAddress()));
          $orderObj-&gt;setShippingAddress($convertQuoteObj-&gt;addressToOrderAddress($quoteObj-&gt;getShippingAddress()));
  
          // set payment options
          $orderObj-&gt;setPayment($convertQuoteObj-&gt;paymentToOrderPayment($quoteObj-&gt;getPayment()));
          if ($paymentData) {
          $orderObj-&gt;getPayment()-&gt;setCcNumber($paymentData-&gt;ccNumber);
          $orderObj-&gt;getPayment()-&gt;setCcType($paymentData-&gt;ccType);
          $orderObj-&gt;getPayment()-&gt;setCcExpMonth($paymentData-&gt;ccExpMonth);
          $orderObj-&gt;getPayment()-&gt;setCcExpYear($paymentData-&gt;ccExpYear);
          $orderObj-&gt;getPayment()-&gt;setCcLast4(substr($paymentData-&gt;ccNumber,-4));
          }
          // convert quote items
          foreach ($items as $item) {
              // @var $item Mage_Sales_Model_Quote_Item
              $orderItem = $convertQuoteObj-&gt;itemToOrderItem($item);
  
              $options = array();
          if ($productOptions = $item-&gt;getProduct()-&gt;getTypeInstance(true)-&gt;getOrderOptions($item-&gt;getProduct())) {
  
              $options = $productOptions;
          }
          if ($addOptions = $item-&gt;getOptionByCode('additional_options')) {
              $options['additional_options'] = unserialize($addOptions-&gt;getValue());
          }
          if ($options) {
              $orderItem-&gt;setProductOptions($options);
          }
              if ($item-&gt;getParentItem()) {
                  $orderItem-&gt;setParentItem($orderObj-&gt;getItemByQuoteItemId($item-&gt;getParentItem()-&gt;getId()));
              }
              $orderObj-&gt;addItem($orderItem);
          }
  
          $orderObj-&gt;setCanShipPartiallyItem(false);
  
          try {
              $orderObj-&gt;place();
          } catch (Exception $e){     
              Mage::log($e-&gt;getMessage());
              Mage::log($e-&gt;getTraceAsString());
          }
  
          $orderObj-&gt;save(); 
          //$orderObj-&gt;sendNewOrderEmail(); 
          return $orderObj-&gt;getId();
  
      }
  </code></pre>
  
  <p>The process works fine, no errors, and the order is created. But the total is 0 and there are no products in it no matter what I put.</p>
  
  <p>I've traced it and I can confirm that the rows are added to the <code>sales_flat_quote</code> and <code>sales_flat_quote_item</code> tables, so that is ok. But when running the <code>createOrder</code> and calling</p>
  
  <pre><code>     $items = $quoteObj-&gt;getAllItems();
  </code></pre>
  
  <p>an empty array is always returned, and I have no idea why. I have configurable and simple products in my shop. This happens when I add simple, when I add configurable the error appears as the method </p>
  
  <pre><code>    $quoteItem = $quoteObj-&gt;addProduct($productObj);
  </code></pre>
  
  <p>returns null.</p>