<?php
$order = $orderData;
$billingAddress = $order['billing_address'];
$shippingAddress = $order['shipping_address'];
$quote = Mage::getModel('sales/quote');
$customer = Mage::getModel('customer/customer');
$customer->setWebsiteId(Mage::app()->getWebsite()->getId());
$customer->loadByEmail($order['customer_email']);
if($customer->getId()){
$quote->assignCustomer($customer);
}
else
{
$quote->setIsMultiShipping(false);
$quote->setCheckoutMethod('guest');
$quote->setCustomerId(null);
$quote->setCustomerEmail($order['customer_email']);
$quote->setCustomerIsGuest(true);
$quote->setCustomerGroupId(Mage_Customer_Model_Group::NOT_LOGGED_IN_ID);
}
$quote->setStore(Mage::app()->getStore());
$product = Mage::getModel('catalog/product');
foreach($order['items'] as $item) {
$productId = Mage::getModel('catalog/product')->getIdBySku($item['sku']);
$product->load($productId);
$quoteItem = Mage::getModel('sales/quote_item')->setProduct($product);
$quoteItem->setQuote($quote);
$quoteItem->setQty($item['qty_ordered']);
$quote->addItem($quoteItem);
}
$addressForm = Mage::getModel('customer/form');
$addressForm->setFormCode('customer_address_edit')
->setEntityType('customer_address');
foreach ($addressForm->getAttributes() as $attribute) {
if (isset($shippingAddress[$attribute->getAttributeCode()])) {
$quote->getShippingAddress()->setData($attribute->getAttributeCode(), $shippingAddress[$attribute->getAttributeCode()]);
}
}
foreach ($addressForm->getAttributes() as $attribute) {
if (isset($billingAddress[$attribute->getAttributeCode()])) {
$quote->getBillingAddress()->setData($attribute->getAttributeCode(), $billingAddress[$attribute->getAttributeCode()]);
}
}
$quote->getShippingAddress()->setShippingMethod($order['shipping_method']);
$quote->getShippingAddress()->setCollectShippingRates(true);
$quote->getShippingAddress()->collectShippingRates();
$quote->collectTotals();
$quote->save();
$items = $quote->getAllItems();
$quote->reserveOrderId();
$quotePayment = $quote->getPayment(); // Mage_Sales_Model_Quote_Payment
$quotePayment->setMethod($order['payment']['method']);
$quote->setPayment($quotePayment);
$convertQuote = Mage::getSingleton('sales/convert_quote');
$order = $convertQuote->addressToOrder($quote->getShippingAddress());
$orderPayment = $convertQuote->paymentToOrderPayment($quotePayment);
$order->setBillingAddress($convertQuote->addressToOrderAddress($quote->getBillingAddress()));
$order->setShippingAddress($convertQuote->addressToOrderAddress($quote->getShippingAddress()));
$order->setPayment($convertQuote->paymentToOrderPayment($quote->getPayment()));
/*
$order->getPayment()->setCcNumber($paymentData->ccNumber);
$order->getPayment()->setCcType($paymentData->ccType);
$order->getPayment()->setCcExpMonth($paymentData->ccExpMonth);
$order->getPayment()->setCcExpYear($paymentData->ccExpYear);
$order->getPayment()->setCcLast4(substr($paymentData->ccNumber,-4));
*/
foreach ($items as $item) {
$orderItem = $convertQuote->itemToOrderItem($item);
if ($item->getParentItem()) {
$orderItem->setParentItem($order->getItemByQuoteItemId($item->getParentItem()->getId()));
}
$order->addItem($orderItem);
}
try {
$order->place();
$order->save();
} catch (Exception $e){
Mage::log($e->getMessage());
}
?>