Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

How do I create an order, programmatically? 
 
EYEMAGINE
Member
 
Avatar
Total Posts:  35
Joined:  2008-02-14
Irvine, CA
 

Hi all,

I’m trying to import orders from a 3rd party store.

I’ve tried looking for the code that creates a new order, but I keep getting lost in the Magento code.

I would like to create an order object and save it with the appropriate products and shipping option.  I would also like to associate the payment method.

This is the code I have so far.  When I run it, it creates an order record in the admin, but when I click on the newly created order (from the admin), I get an error about the Mage_Payment_Model_Info missing.

$query "SELECT orders FROM <3rd party system>";
$resultSet mysql_query($query$conn);

while (
$orderRecord mysql_fetch_array($resultSet)) {

    
// get the customer's ID from the magento database using a query here
    // so we can use the ID to instantiate a customer object
    
$query "SELECT entity_id FROM roddenberry_magento.customer_entity WHERE email = '{$orderRecord['strEmail']}'";
    
$customerIdResultSet mysql_query($query$conn);
    
$customerIdData mysql_fetch_array($customerIdResultSet);
    
    
$customer Mage::getModel('customer/customer');
    
$customer->load($customerIdData['entity_id']);
    
$customerAddresses $customer->getAddresses();
    
$billingAddress $customerAddresses[0];
    
$shippingAddress $customerAddresses[0];

    
// select all of the products from this order
    
$query "SELECT items FROM <3rd party system>";
    
$resultSetProducts mysql_query($query$conn);
    
    
// create the quote
    
$quote Mage::getModel('sales/quote');
    
$quote->assignCustomer($customer);
    
$quote->setCustomer($customer);
    
    
// add products to the quote
    
while ($orderItemRecord mysql_fetch_array($resultSetProducts)) {
    
        
// get the product ID by the SKU
        
$query "SELECT entity_id FROM catalog_product_entity WHERE sku='{$orderItemRecord['strSKU']}'";
        
$productIdResultSet mysql_query($query$conn);
        
$productIdData mysql_fetch_array($productIdResultSet);
        
        
// create quote item object for each product
        
$product Mage::getModel('catalog/product');
        
$product->load($productIdData['entity_id']);
        
$quoteItem Mage::getModel('sales/quote_item');
        
$quoteItem->importCatalogProduct($product);
        
$quoteItem->setQty($orderItemRecord['intQuantity']);
        
        
// add quote item to quote
        
$quoteItem->setQuote($quote);
    
}
    
    
//get a new order object
    
$order Mage::getModel('sales/order');
    
$order->setStoreId(1); 

    
$data = array("created_at" => $orderRecord['dtInsert'],
                  
"updated_at" => $orderRecord['dtShipped'],
                  
"is_active" => 1,
                  
"tax_amount" => $orderRecord['dblTaxes'],
                  
"shipping_amount" => $orderRecord['dblShipping'],
                  
"discount_amount" => $orderRecord['dblDiscount'],
                  
"subtotal" => $orderRecord['dblSubTotal'],
                  
"grand_total" => $orderRecord['dblTotal'],
                  
"total_paid" => ($orderRecord['blnPaid']) ? $orderRecord['dblTotal''0.000',
                  
"total_refunded" => "0.0000",
                  
"total_canceled" => "0.0000",
                  
"total_online_refunded" => "0.0000",
                  
"total_offline_refunded" => "0.0000",
                  
"subtotal_refunded" => NULL,
                  
"subtotal_canceled" => NULL,
                  
"tax_refunded" => NULL,
                  
"tax_canceled" => NULL,
                  
"shipping_refunded" => NULL,
                  
"shipping_canceled" => NULL,
                  
"base_subtotal_refunded" => NULL,
                  
"base_subtotal_canceled" => NULL,
                  
"base_tax_refunded" => NULL,
                  
"base_tax_canceled" => NULL,
                  
"base_shipping_refunded" => NULL,
                  
"base_shipping_canceled" => NULL,
                  
"subtotal_invoiced" => NULL,
                  
"tax_invoiced" => NULL,
                  
"shipping_invoiced" => NULL,
                  
"base_subtotal_invoiced" => NULL,
                  
"base_tax_invoiced" => NULL,
                  
"base_shipping_invoiced" => NULL,
                  
"base_currency_code" => "USD",
                  
"store_currency_code" => "USD",
                  
"order_currency_code" => "USD",
                  
"applied_rule_ids" => "",
                  
"shipping_method" => $orderRecord['strShipping'],
                  
"shipping_description" => $orderRecord['strShipping'],
                  
"state" => "new",
                  
"status" => $orderStatus,
                  
"store_name" => "Main Website
Main Store
English"
,
                  
"customer_note_notify" => "1",
                  
"is_virtual" => "0",
                  
"is_multi_payment" => "0",
                  
"email_sent" => "1",
                  
"billing_address_id" => $billingAddress->getId(),
                  
"shipping_address_id" => $shippingAddress->getId(),
                  
"store_to_base_rate" => "1.0000",
                  
"store_to_order_rate" => "1.0000",
                  
"weight" => "",
                  
"custbalance_amount" => "0.0000",
                  
"base_custbalance_amount" => "0.0000",
                  
"customer_note" => $orderRecord['strAdminNotes']);
    
        
$order->addData($data);
    
    try 
{
        $order
->save();
        echo 
"Success! Id: ".$order->getId();
    
}
    
catch (Exception $e){
        
echo $e->getMessage();
    
}
    
    
exit;

Can anyone help by telling me if this is the right path to go down or should I create orders and associate products with them differently?

It’s getting close to launch date and I need to migrate these orders.

I really appreciate the help!

Andy

 
Magento Community Magento Community
Magento Community
Magento Community
 
EYEMAGINE
Member
 
Avatar
Total Posts:  35
Joined:  2008-02-14
Irvine, CA
 

I kind of figured it out.  I wrote an order migration script that worked for a proprietary e-Commerce database.

I’ve attached the script.  To use it, you’ll have to modify it to work with your previous e-Commerce database.

Andy

File Attachments
orders_for_magento.php  (File Size: 12KB - Downloads: 636)
 
Magento Community Magento Community
Magento Community
Magento Community
 
sotrue
Jr. Member
 
Total Posts:  1
Joined:  2013-02-08
 

I am not able to download you file please upload it again .

EYEMAGINE - 14 July 2008 09:03 AM

I kind of figured it out.  I wrote an order migration script that worked for a proprietary e-Commerce database.

I’ve attached the script.  To use it, you’ll have to modify it to work with your previous e-Commerce database.

Andy

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top