我在Magento管理员中以编程方式创建订单,方法是从引号转换并以编程方式设置所有数据。但是,这种订单的问题在于,当我尝试编辑它们时,新订单没有获得旧订单中的信息,例如产品,付款方式等。我是否需要为订单设置特殊标志或其他东西,以便能够以与编辑标准Magento订单相同的方式进行编辑?请指导。
这是我用来创建订单的代码。
require_once 'app/Mage.php';
Mage::app();
$quoteId = $this->getRequest()->getParam('quote_id');
$quote = Mage::getModel('sales/quote')->loadByIdWithoutStore($quoteId);
$id = $quote->customer_id;
$itemsCount = $quote->items_count;
$customer = Mage::getModel('customer/customer')->load($id);
$userEmail = $customer->getEmail();
$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
$currencyCode = Mage::app()->getStore()->getCurrentCurrencyCode();
$order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setStoreId($storeId)
->setQuoteId($quoteId)
->setGlobal_currency_code($currencyCode)
->setBase_currency_code($currencyCode)
->setStore_currency_code($currencyCode)
->setOrder_currency_code($currencyCode);
$order->setCustomer_email($customer->getEmail())
->setCustomerFirstname($customer->getFirstname())
->setCustomerLastname($customer->getLastname())
->setCustomerGroupId($customer->getGroupId())
->setCustomer_is_guest(0)
->setCustomer($customer);
$billing = $customer->getDefaultBillingAddress();
if ($billing == "") {
$billing = $quote->getBillingAddress()->exportCustomerAddress();
}
$shipping = $customer->getDefaultShippingAddress();
if ($shpping == "") {
$shpping = $quote->getShippingAddress()->exportCustomerAddress();
}
$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);
if ($shipping == "") {
$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($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());
} else {
$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());
}
$shippingMethode = $quote->getShippingAddress()->getShippingMethod();
$shippingDescription = $quote->getShippingAddress()->getShippingDescription();
$order->setShippingAddress($shippingAddress)
->setShipping_method($shippingMethode)
->setShippingDescription($shippingDescription);
$paymentcode = $quote->getPayment()->getMethodInstance()->getCode();
$payData = $quote->getPayment()->getData();
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod($paymentcode)->addData($payData);
$order->setPayment($orderPayment);
$subTotal = 0;
$orderData = Mage::getModel('sales/quote')->loadByIdWithoutStore($quoteId);
$itemsData = $orderData->getAllItems();
foreach ($itemsData as $itemIds => $itemValue) {
$products[$itemValue->getProductId()] = array('qty' => $itemValue->getQty());
$rowTotal = $itemValue->getPrice() * $itemValue->getQty();
$orderItem = Mage::getModel('sales/order_item')
->setStoreId($storeId)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setProductId($itemValue->getProductId())
->setProductType($itemValue->getTypeId())
->setQtyBackordered(NULL)
->setTotalQtyOrdered($itemValue->getRqty())
->setQtyOrdered($itemValue->getQty())
->setName($itemValue->getName())
->setSku($itemValue->getSku())
->setPrice($itemValue->getPrice())
->setBasePrice($itemValue->getPrice())
->setOriginalPrice($itemValue->getPrice())
->setRowTotal($rowTotal)
->setBaseRowTotal($rowTotal);
$subTotal += $rowTotal;
$order->addItem($orderItem);
}
$shippingAmount = $quote->getShippingAddress()->getShippingAmount();
$subTotal = $quote->getSubtotal();
$baseTotal = $quote->getBaseSubtotal();
$grandTotal = $quote->getGrandTotal();
$baseGrandTotal = $quote->getBaseGrandTotal();
$order->setSubtotal($subTotal)
->setBaseSubtotal($baseTotal)
->setGrandTotal($grandTotal)
->setBaseGrandTotal($baseGrandTotal);
$order->setShippingAmount($shippingAmount);
$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));
$transaction->save();
$quote = $this->_initQuote();
$orderId = $order->getEntityId();
$requestModel = Mage::getModel('quote2sales/request');
$requestIdArray = $requestModel->getQuoteData($quoteId);
$requestId = $requestIdArray[0]['request_id'];
if ($requestId != NULL) {
$requestModel->updateRequestStatus("Converted To Order", $requestId);
$requestModel->addOrderId("Converted To Order", $quoteId, $orderId);
}
$mailer = Mage::getModel('core/email_template_mailer');
$emailInfo = Mage::getModel('core/email_info');
$emailInfo->addTo($userEmail);
$mailer->addEmailInfo($emailInfo);
$templateId = Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_TEMPLATE, $storeId);
$mailer->setSender(Mage::getStoreConfig(Mage_Sales_Model_Order::XML_PATH_EMAIL_IDENTITY, $storeId));
$mailer->setStoreId($storeId);
$mailer->setTemplateId($templateId);
$mailer->setTemplateParams(
array(
'order' => $order,
'billing' => $order->getBillingAddress(),
'payment_html' => ""
)
);
$mailer->send();
if ($quote) {
try {
$quote->delete()
->save();
} catch (Mage_Core_Exception $e) {
} catch (Exception $e) {
Mage::logException($e);
}
Mage::getSingleton('adminhtml/session')->addSuccess($this->__('The Order has been created.'));
$this->_redirect('*/adminhtml_quote/index');
} else {
Mage::log("Can't get quote");
}