2011-11-08 78 views
5

在這個話題上似乎沒有堅實的共識。有一個線程on the Magento message board,它提供了各種變化和方法,這些都不適用於我,沒有一個解釋在這個過程中應該做什麼。如何以編程方式將訂單導入Magento?

從我所知道的情況來看,您需要模擬在網站上創建訂單的步驟,即將產品添加到「購物車」,添加送貨和帳單地址,添加送貨方式,添加付款方式和'結帳'。

有人可以請解釋這些步驟,並顯示負責執行步驟的代碼行。

此外,我在參考消息板代碼中的'購物車'和'報價'的例子。請解釋2.

+0

我問了一個有效的問題。爲什麼你會浪費你的時間發佈廢話呢?誰下地投了我的問題? – Billy

+0

我的觀點是,有人需要撰寫相當於博客文章的文章才能充分回答您在問題中提出的所有問題。而且,寫一句話確實不會佔用我太多的時間。在這個網站上,一個問題通常不需要超過30分鐘的答案。 –

+0

我在打字時間的10分鐘內回答了我的一半問題。 – Billy

回答

6

之間的差異(或相似之處)我已經完成了編程任務。事實證明,您不必像別處的其他帖子所暗示的那樣模擬「添加到購物車」。您可以創建訂單對象和相關對象並填充數據,但它不像我希望的那樣簡單。

創建訂單最好由this blog post來描述。

我在這裏複製的代碼:

$id=1; // get Customer Id 
$customer = Mage::getModel('customer/customer')->load($id); 

$transaction = Mage::getModel('core/resource_transaction'); 
$storeId = $customer->getStoreId(); 
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId); 

$order = Mage::getModel('sales/order') 
    ->setIncrementId($reservedOrderId) 
    ->setStoreId($storeId) 
    ->setQuoteId(0) 
    ->setGlobal_currency_code('USD') 
    ->setBase_currency_code('USD') 
    ->setStore_currency_code('USD') 
    ->setOrder_currency_code('USD'); 

// set Customer data 
$order->setCustomer_email($customer->getEmail()) 
    ->setCustomerFirstname($customer->getFirstname()) 
    ->setCustomerLastname($customer->getLastname()) 
    ->setCustomerGroupId($customer->getGroupId()) 
    ->setCustomer_is_guest(0) 
    ->setCustomer($customer); 

// set Billing Address 
$billing = $customer->getDefaultBillingAddress(); 
$billingAddress = Mage::getModel('sales/order_address') 
    ->setStoreId($storeId) 
    ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING) 
    ->setCustomerId($customer->getId()) 
    ->setCustomerAddressId($customer->getDefaultBilling()) 
    ->setCustomer_address_id($billing->getEntityId()) 
    ->setPrefix($billing->getPrefix()) 
    ->setFirstname($billing->getFirstname()) 
    ->setMiddlename($billing->getMiddlename()) 
    ->setLastname($billing->getLastname()) 
    ->setSuffix($billing->getSuffix()) 
    ->setCompany($billing->getCompany()) 
    ->setStreet($billing->getStreet()) 
    ->setCity($billing->getCity()) 
    ->setCountry_id($billing->getCountryId()) 
    ->setRegion($billing->getRegion()) 
    ->setRegion_id($billing->getRegionId()) 
    ->setPostcode($billing->getPostcode()) 
    ->setTelephone($billing->getTelephone()) 
    ->setFax($billing->getFax()); 
$order->setBillingAddress($billingAddress); 

$shipping = $customer->getDefaultShippingAddress(); 
$shippingAddress = Mage::getModel('sales/order_address') 
    ->setStoreId($storeId) 
    ->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING) 
    ->setCustomerId($customer->getId()) 
    ->setCustomerAddressId($customer->getDefaultShipping()) 
    ->setCustomer_address_id($shipping->getEntityId()) 
    ->setPrefix($shipping->getPrefix()) 
    ->setFirstname($shipping->getFirstname()) 
    ->setMiddlename($shipping->getMiddlename()) 
    ->setLastname($shipping->getLastname()) 
    ->setSuffix($shipping->getSuffix()) 
    ->setCompany($shipping->getCompany()) 
    ->setStreet($shipping->getStreet()) 
    ->setCity($shipping->getCity()) 
    ->setCountry_id($shipping->getCountryId()) 
    ->setRegion($shipping->getRegion()) 
    ->setRegion_id($shipping->getRegionId()) 
    ->setPostcode($shipping->getPostcode()) 
    ->setTelephone($shipping->getTelephone()) 
->setFax($shipping->getFax()); 

$order->setShippingAddress($shippingAddress) 
    ->setShipping_method('flatrate_flatrate') 
    ->setShippingDescription($this->getCarrierName('flatrate')); 

$orderPayment = Mage::getModel('sales/order_payment') 
    ->setStoreId($storeId) 
    ->setCustomerPaymentId(0) 
    ->setMethod('purchaseorder') 
    ->setPo_number(' - '); 
$order->setPayment($orderPayment); 

// let say, we have 2 products 
$subTotal = 0; 
    $products = array(
    '1001' => array(
    'qty' => 1 
), 
    '1002' ->array(
    'qty' => 3 
), 
); 
foreach ($products as $productId=>$product) { 
    $_product = Mage::getModel('catalog/product')->load($productId); 
    $rowTotal = $_product->getPrice() * $product['qty']; 
    $orderItem = Mage::getModel('sales/order_item') 
    ->setStoreId($storeId) 
    ->setQuoteItemId(0) 
    ->setQuoteParentItemId(NULL) 
    ->setProductId($productId) 
    ->setProductType($_product->getTypeId()) 
    ->setQtyBackordered(NULL) 
    ->setTotalQtyOrdered($product['rqty']) 
    ->setQtyOrdered($product['qty']) 
    ->setName($_product->getName()) 
    ->setSku($_product->getSku()) 
    ->setPrice($_product->getPrice()) 
    ->setBasePrice($_product->getPrice()) 
    ->setOriginalPrice($_product->getPrice()) 
    ->setRowTotal($rowTotal) 
    ->setBaseRowTotal($rowTotal); 

    $subTotal += $rowTotal; 
    $order->addItem($orderItem); 
} 

$order->setSubtotal($subTotal) 
    ->setBaseSubtotal($subTotal) 
    ->setGrandTotal($subTotal) 
    ->setBaseGrandTotal($subTotal); 

$transaction->addObject($order); 
$transaction->addCommitCallback(array($order, 'place')); 
$transaction->addCommitCallback(array($order, 'save')); 
$transaction->save(); 

我還沒有得到區別的購物車和報價之間的理解。

相關問題