2011-07-29 43 views
14

Tôi đang sử dụng hai phương pháp này để tạo đơn đặt hàng theo trình tự trong Magento.Tạo đơn đặt hàng theo chương trình trong Magento

Người đầu tiên tạo ra một 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(); 

    } 

Và điều thứ hai sử dụng mà Quote tạo theo thứ tự:

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

    } 

Quá trình này hoạt động tốt, không có lỗi, và thứ tự được tạo ra. Nhưng tổng số là 0 và không có sản phẩm nào trong đó dù tôi có đặt gì đi nữa.

Tôi đã theo dõi nó và tôi có thể xác nhận rằng các hàng được thêm vào các bảng sales_flat_quotesales_flat_quote_item, vì vậy điều đó là ok. Nhưng khi chạy createOrder và gọi

 $items = $quoteObj->getAllItems(); 

một mảng trống luôn được trả về, và tôi không có ý tưởng tại sao. Tôi có các sản phẩm cấu hình và đơn giản trong cửa hàng của mình. Điều này xảy ra khi tôi thêm đơn giản, khi tôi thêm cấu hình lỗi xuất hiện dưới dạng phương thức

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

trả về giá trị rỗng.

+0

tôi có một vấn đề tương tự bạn đã tìm thấy một giải pháp – rosh3000

+0

khách hàng gì ID bạn sẽ sử dụng cho một thanh toán của khách? – swl1020

Trả lời

9

Dường như với tôi, bạn không tải bộ sưu tập sản phẩm, do đó, giỏ hàng luôn trả về sản phẩm nào. Hãy thử liên kết này, nó sẽ giúp bạn rõ ràng hơn.

// 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

Mã này làm việc cho tôi,

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

Tính năng này có hoạt động đối với Sản phẩm có thể định cấu hình không? –

0

tôi đã cùng một vấn đề và delved vào API để tìm một giải pháp. Tôi đã thay đổi cách mà tôi nạp một sản phẩm bằng cách sử dụng:

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

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

tôi thấy hướng dẫn này là rất hữu ích quá:

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

Nếu bạn đang tìm kiếm một kịch bản trên tạo đơn đặt hàng này là một khởi đầu rất tốt:

http://pastebin.com/8cft4d8v

Hy vọng rằng điều này giúp ai đó;)

Các vấn đề liên quan