创建订单在Magento编程


Create order programmatically in Magento

我使用这两个方法在Magento中以编程方式创建订单。

第一个创建了一个Quote:

 public function prepareCustomerOrder($customerId, array $shoppingCart, array  $shippingAddress, array $billingAddress, 
        $shippingMethod, $couponCode = null) 
    {
        $customerObj = Mage::getModel('customer/customer')->load($customerId);
        $storeId = $customerObj->getStoreId();
        $quoteObj = Mage::getModel('sales/quote')->assignCustomer($customerObj);
        $storeObj = $quoteObj->getStore()->load($storeId);
        $quoteObj->setStore($storeObj);
        // add products to quote
        foreach($shoppingCart as $part) {
            $productModel = Mage::getModel('catalog/product');
            $productObj = $productModel->setStore($storeId)->setStoreId($storeId)->load($part['PartId']);
            $productObj->setSkipCheckRequiredOption(true);
            try{
                $quoteItem = $quoteObj->addProduct($productObj);
                $quoteItem->setPrice(20);
                $quoteItem->setQty(3);
                $quoteItem->setQuote($quoteObj);                                    
                $quoteObj->addItem($quoteItem);
            } catch (exception $e) {
            return false;
            }
            $productObj->unsSkipCheckRequiredOption();
            $quoteItem->checkData();
        }
        // addresses
        $quoteShippingAddress = new Mage_Sales_Model_Quote_Address();
        $quoteShippingAddress->setData($shippingAddress);
        $quoteBillingAddress = new Mage_Sales_Model_Quote_Address();
        $quoteBillingAddress->setData($billingAddress);
        $quoteObj->setShippingAddress($quoteShippingAddress);
        $quoteObj->setBillingAddress($quoteBillingAddress);
        // coupon code
        if(!empty($couponCode)) $quoteObj->setCouponCode($couponCode); 

        // shipping method an collect
        $quoteObj->getShippingAddress()->setShippingMethod($shippingMethod);
        $quoteObj->getShippingAddress()->setCollectShippingRates(true);
        $quoteObj->getShippingAddress()->collectShippingRates();
        $quoteObj->collectTotals(); // calls $address->collectTotals();
        $quoteObj->setIsActive(0);
        $quoteObj->save();
        return $quoteObj->getId();
    }

第二个使用Quote创建Order:

 public function createOrder($quoteId, $paymentMethod, $paymentData) 
    {
        $quoteObj = Mage::getModel('sales/quote')->load($quoteId); // Mage_Sales_Model_Quote
        $items = $quoteObj->getAllItems();                  
        $quoteObj->reserveOrderId();
          // set payment method 
        $quotePaymentObj = $quoteObj->getPayment(); // Mage_Sales_Model_Quote_Payment
        $quotePaymentObj->setMethod($paymentMethod);
        $quoteObj->setPayment($quotePaymentObj);
        // convert quote to order
        $convertQuoteObj = Mage::getSingleton('sales/convert_quote');
        $orderObj = $convertQuoteObj->addressToOrder($quoteObj->getShippingAddress());
        $orderPaymentObj = $convertQuoteObj->paymentToOrderPayment($quotePaymentObj);
        // convert quote addresses
        $orderObj->setBillingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getBillingAddress()));
        $orderObj->setShippingAddress($convertQuoteObj->addressToOrderAddress($quoteObj->getShippingAddress()));
        // set payment options
        $orderObj->setPayment($convertQuoteObj->paymentToOrderPayment($quoteObj->getPayment()));
        if ($paymentData) {
        $orderObj->getPayment()->setCcNumber($paymentData->ccNumber);
        $orderObj->getPayment()->setCcType($paymentData->ccType);
        $orderObj->getPayment()->setCcExpMonth($paymentData->ccExpMonth);
        $orderObj->getPayment()->setCcExpYear($paymentData->ccExpYear);
        $orderObj->getPayment()->setCcLast4(substr($paymentData->ccNumber,-4));
        }
        // convert quote items
        foreach ($items as $item) {
            // @var $item Mage_Sales_Model_Quote_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);
        }
        $orderObj->setCanShipPartiallyItem(false);
        try {
            $orderObj->place();
        } catch (Exception $e){     
            Mage::log($e->getMessage());
            Mage::log($e->getTraceAsString());
        }
        $orderObj->save(); 
        //$orderObj->sendNewOrderEmail(); 
        return $orderObj->getId();
    }

该过程运行良好,没有错误,并且创建了订单。但是总的结果是0,不管我放什么,里面都没有乘积。

我已经跟踪了它,我可以确认这些行被添加到sales_flat_quotesales_flat_quote_item表中,所以这是可以的。但是当运行createOrder并调用

     $items = $quoteObj->getAllItems();

总是返回一个空数组,我不知道为什么。我有可配置的和简单的产品在我的商店。当我添加simple时,当我添加configurable时错误出现在

方法中
    $quoteItem = $quoteObj->addProduct($productObj);

返回null。

在我看来,您没有加载产品集合,因此,购物车总是返回空。试试这个链接,它会给你更明确的帮助。以编程方式创建订单

// this is get only one product, you can refactor the code
$this->_product = Mage::getModel('catalog/product')->getCollection()
  ->addAttributeToFilter('sku', 'Some value here...')
  ->addAttributeToSelect('*')
  ->getFirstItem();
// load product data
$this->_product->load($this->_product->getId());

这段代码为我工作,

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

我遇到了同样的问题,并深入研究了API以找到解决方案。我改变了加载产品的方式,使用:

$productEntityId = '123456';   
$store_code = 'my_store_code';
$product = Mage::helper('catalog/product')->getProduct($productEntityId,Mage::app()->getStore($store_code)->getId());

我发现这个教程也很有用:

http://www.classyllama.com/content/unravelling-magentos-collecttotals

如果你正在寻找一个关于订单创建的脚本,这是一个很好的开始:

http://pastebin.com/8cft4d8v

希望对大家有所帮助;)