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

programmatically create order in magento
 
heaven dweller
Jr. Member
 
Total Posts:  18
Joined:  2009-11-09
 

$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());
}




?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
Setti
Jr. Member
 
Total Posts:  6
Joined:  2010-05-26
 

Спасибо большое! Мне очень помог ваш фрагмент кода!

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