2013-03-11 109 views
0

我正在创建一个magento扩展,其中我需要从购物车以编程方式创建订单。我正在使用下面的代码来达到这个目的。但我无法实现它的工作。任何帮助,将不胜感激。Magento:以编程方式创建订单中的问题

// Check whether the product could be loaded 
if(isset($_POST['create_order'])){ 
    // Get the customer model 
    $customer = Mage::getModel('customer/customer'); 

    // Set the website id associated with the customer 
    $customer->setWebsiteId(Mage::app()->getWebsite()->getId()); 

if($_POST['customer'] == 'exist') 
    $email = $_POST['mageUser']['email']; 
else 
    $email = $_POST['mageUser']['email_id']; 

    // Try to load the customer by email 
    $customer->loadByEmail($email); 

    // Check whether the customer not exists 
    if(!$customer->getId()) 
    { 
     // Create the customer 
     $customer->setEmail($email); 
     $customer->setFirstname($_POST['mageUser']['first_name']); 
     $customer->setLastname($_POST['mageUser']['last_name']); 
     $customer->save(); 
     $fname = $_POST['mageUser']['first_name']; 
     $lname = $_POST['mageUser']['last_name']; 
    } 
    else { 
    $fname = $customer->getFirstname(); 
    $lname = $customer->getLastname(); 
    } 

    // Set the esstial order data 
    $orderData = array(
     'currency' => 'USD', 
     'account' => array(
      'group_id' => Mage_Customer_Model_Group::NOT_LOGGED_IN_ID, 
      'email' => $email 
    ), 
     'billing_address' => array(
      'firstname' => $fname, 
      'lastname' => $lname, 
      'street'  => 'pos', 
      'city'  => 'Pos', 
      'country_id' => '54', 
      'region_id' => 'BW', 
      'postcode' => 'pos', 
      'telephone' => 'pos', 
    ), 
     'comment' => array(
      'customer_note' => "Order placed by point of sales\n" 
    ), 
     'send_confirmation' => false 
); 

    // Set the shipping address to the billing address 
    $orderData['shipping_address'] = $orderData['billing_address']; 

    // Set the payment method 
    $paymentMethod = 'pay'; 

    // Set the shipping method 
    $shippingMethod = 'freeshipping_freeshipping'; 


    // Get the backend quote session 
    $quoteSession = Mage::getSingleton('adminhtml/session_quote'); 

    // Set the session store id 
    $quoteSession->setStoreId(Mage::app()->getStore('default')->getId()); 

    // Set the session customer id 
    $quoteSession->setCustomerId($customer->getId()); 


    // Get the backend order create model 
    $orderCreate = Mage::getSingleton('adminhtml/sales_order_create'); 

    // Import the data 
    $orderCreate->importPostData($orderData); 

    // Calculate the shipping rates 
    $orderCreate->collectShippingRates(); 

    // Set the shipping method 
    $orderCreate->setPaymentMethod($paymentMethod); 

    // Set the payment method to the payment instance 
    $orderCreate->getQuote()->getPayment()->addData(array('method' => $paymentMethod)); 

    // Set the shipping method 
    $orderCreate->setShippingMethod($shippingMethod); 

    // Set the quote shipping address shipping method 
    $orderCreate->getQuote()->getShippingAddress()->setShippingMethod($shippingMethod); 

    // Add the product 
    $quote = Mage::getSingleton('checkout/session')->getQuote(); 
    $cartItems = $quote->getAllVisibleItems(); 
    $prd = array(); 
    foreach ($cartItems as $item) { 
    $prd[$item->getId()] = array('qty' => $item->getQty()); 
    } 

    $orderCreate->addProducts($prd); 
    //print_r($orderCreate); exit(); 
    // Initialize data for price rules 
    $orderCreate->initRuleData(); 

    // Save the quote 
    $orderCreate->saveQuote(); 

    // Create the order 
    $order = $orderCreate->createOrder(); 
    $order->setData('state', "complete"); 
    $order->setStatus("complete"); 
    $history = $order->addStatusHistoryComment('Order marked as complete automatically.', false); 
    $history->setIsCustomerNotified(false); 
    $order->save(); 
} 

它显示此错误消息当我打开开发人员模式

#0 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php(1614): Mage::throwException('') 
#1 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/code/core/Mage/Adminhtml/Model/Sales/Order/Create.php(1508): Mage_Adminhtml_Model_Sales_Order_Create->_validate() 
#2 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/code/local/Nettantra/Pos/controllers/IndexController.php(210): Mage_Adminhtml_Model_Sales_Order_Create->createOrder() 
#3 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/code/core/Mage/Core/Controller/Varien/Action.php(419): Nettantra_Pos_IndexController->createorderAction() 
#4 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(250): Mage_Core_Controller_Varien_Action->dispatch('createorder') 
#5 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/code/core/Mage/Core/Controller/Varien/Front.php(176): Mage_Core_Controller_Varien_Router_Standard->match(Object(Mage_Core_Controller_Request_Http)) 
#6 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/code/core/Mage/Core/Model/App.php(354): Mage_Core_Controller_Varien_Front->dispatch() 
#7 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/app/Mage.php(683): Mage_Core_Model_App->run(Array) 
#8 /home/seat2/public_html/Projects/MagentoPos/Repo/WebApp/index.php(87): Mage::run('', 'store') 
#9 {main} 
+0

然后,什么是问题,错误信息,...? – Amir 2013-03-11 06:57:41

+0

我已经添加了该问题,请检查 – 2013-03-11 07:03:23

回答

0
$productModel = Mage::getModel('catalog/product'); 
$productId = $productModel->getIdBySku($sku); 
$product = $productModel->load($productId); 
$cart = Mage::getModel('checkout/cart'); 
$cart->init(); 
$cart->addProduct($product, array('qty' => $quantity)); 
$cart->save(); 

**您需要定义$ SKU和$数量 这将创建一个购物车并添加它也是产品;希望这能指出你在正确的方向

相关问题