0

我正在创建一个 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}
4

1 回答 1

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 和 $quantity 这将创建一个购物车并添加一个产品;希望这会为您指明正确的方向

于 2014-05-28T21:26:52.017 回答