2011-07-29 3 views
14

Magentoでこれら2つの方法を使ってプログラムで注文を作成しています。Magentoでプログラムで注文を作成する

最初の見積もり作成:

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

    } 

を2つ目は、見積もりが注文を作成することを使用しています。

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

を呼び出すとき私は設定可能で簡単な製品を私の店に持っています。これはシンプルに追加すると発生し、設定可能なエラーがメソッドとして表示されます。

$quoteItem = $quoteObj->addProduct($productObj); 

が返されます。

+0

私は同様の問題を持っているあなたは、ゲストのチェックアウトのために使用するどのような顧客IDソリューション – rosh3000

+0

を見つけましたか? – swl1020

答えて

9

商品の回収は行っておりませんので、カートは必ず空になります。このリンクを試すと、より明確なヘルプが得られます。

// 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()); 
2

このコードは、私のために働いた、

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

設定可能な製品でも機能しますか? –

0

私は同じ問題を抱えていたし、解決策を見つけるためにAPI掘り下げ。あなたは上のスクリプトを探しているなら

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

:私はこのチュートリアルでは、あまりにも非常に有用であることが判明

$productEntityId = '123456'; 
$store_code = 'my_store_code'; 

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

:私は、私が使用して製品をロードする方法を変更しました注文作成は非常に良いスタートです:

http://pastebin.com/8cft4d8v

これは誰かに役立ちますことを願っています。)