简体   繁体   English

Magento:以编程方式为购物车中的商品创建订单

[英]Magento: Create order programmatically for the items on cart

I am trying to create order manually for the cart items and the payment would be completed manually on the spot and the customers would also be created programatically. 我正在尝试为购物车手动创建订单,付款将在现场手动完成,并且还会以编程方式创建客户。 Kind of like a pos. 有点像pos。

I searched a lot and found these 我搜索了很多,发现了这些

http://inchoo.net/ecommerce/magento/programmatically-create-order-in-magento/ http://inchoo.net/ecommerce/magento/programmatically-create-order-in-magento/

http://pastebin.com/8cft4d8v http://pastebin.com/8cft4d8v

Create order programmatically in Magento 在Magento中以编程方式创建订单

http://www.magentocommerce.com/boards/viewthread/294640/ http://www.magentocommerce.com/boards/viewthread/294640/

But none work for me. 但是没有一个对我有用。

public function createorder(array $orderdata)
{
   $quoteId = $orderdata['quoteId'];
   $paymentMethod = $orderdata['paymentMethod'];
   $paymentData = $orderdata['paymentData'];       
   $quoteObj = Mage::getModel('sales/quote')->load($quoteId); 
   $items = $quoteObj->getAllItems();        
   $quoteObj->reserveOrderId();       
   $quotePaymentObj = $quoteObj->getPayment(); 
   $quotePaymentObj->setMethod($paymentMethod);
   $quoteObj->setPayment($quotePaymentObj);        
   $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
   $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());
   $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);       
   $orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
   $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
   $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));  

   foreach ($items as $item) 
    {
       $orderItem = $convertQuoteObj->itemToOrderItem($item);        
       $options = array();
       if ($productOptions = $item->getProduct()->getTypeInstance(true)->getOrderOptions($item->getProduct())) 
       {
         $options = $productOptions;
       }
       if ($addOptions = $item->getOptionByCode('additional_options')) 
       {
        $options['additional_options'] = unserialize($addOptions->getValue());
       }
       if ($options) 
       {
          $orderItem->setProductOptions($options);
       }
       if ($item->getParentItem())
       {
            $orderItem->setParentItem($orderObj->getItemByQuoteItemId($item->getParentItem()->getId()));
       }
       $orderObj->addItem($orderItem);
    }

     $quoteObj->collectTotals();
     $service = Mage::getModel('sales/service_quote', $quoteObj);
     $service->submitAll();
     $orderObj->setCanShipPartiallyItem(false); 

     try 
     {
         $last_order_increment_id = Mage::getModel("sales/order")->getCollection()->getLastItem()->getIncrementId();
         return $last_order_increment_id;

     } 
     catch (Exception $e)
     {     
        Mage::log($e->getMessage());
        Mage::log($e->getTraceAsString());
        return "Exception:".$e;
     }            

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM