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

时间:2013-03-04 07:19:51

标签: php magento magento-1.7

我正在尝试为购物车项目手动创建订单,付款将在现场手动完成,客户也将以编程方式创建。有点像pos。

我搜索了很多并找到了这些

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

http://pastebin.com/8cft4d8v

Create order programmatically in Magento

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

但没有一个对我有用。

1 个答案:

答案 0 :(得分:0)

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;
     }            

}