我会在不破坏其正常流程的情况下遵循 Magento 逻辑。我之前编写的脚本中的代码片段描述了来宾客户的完整结帐流程。你的应该没有太大的不同。请不要复制和粘贴,查看代码并根据您的需要进行调整。
//从发布的表单和购物车和报价模型中加载产品。
$product = Mage::getModel('catalog/product')->load((int) $_POST['product_id']);
$cart = Mage::getSingleton('checkout/cart');
$quote = $cart->getQuote();
//将产品添加到购物车并将会话设置为更新
$params = array('product' => $product->getId(), 'qty' => (int) $_POST['qty']);
$cart->addProduct($product, $params);
$cart->保存();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
//加载订单流程和交易对象需要的变量来保存订单。
$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
//加载订单模型并设置一些默认值
$order = Mage::getModel('销售/订单')
->setIncrementId($reservedOrderId)
->setStoreId($storeId)
->setQuoteId($quote->getId())
->setGlobalCurrencyCode('USD')
->setBaseCurrencyCode('USD')
->setStoreCurrencyCode('USD')
->setOrderCurrencyCode('USD');
//将客户指定为客人,$customer 是来自 $_POST['customer'] 的数组
$order->setCustomerEmail($customer['email'])
->setCustomerFirstname($customer['firstname'])
->setCustomerLastname($customer['lastname'])
->setCustomerGroupId(0)
->setCustomerIsGuest(1);
//准备账单地址并将其添加到订单中
$billingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_BILLING)
->setCustomerId(0)
->setCustomerAddressId(0)
->setCustomerAddressId(0)
->setFirstname($customer['firstname'])
->setLastname($customer['lastname'])
->setStreet($customer['address'])
->setCity($customer['city'])
->setCountryId($customer['country_id'])
->setPostcode($customer['postcode'])
->setTelephone($customer['phone']);
$order->setBillingAddress($billingAddress);
//准备送货地址并添加到订单中,然后设置送货方式
$shippingAddress = Mage::getModel('sales/order_address')
->setStoreId($storeId)
->setAddressType(Mage_Sales_Model_Quote_Address::TYPE_SHIPPING)
->setCustomerId(0)
->setCustomerAddressId(0)
->setCustomerAddressId(0)
->setFirstname($customer['firstname'])
->setLastname($customer['lastname'])
->setStreet($customer['address'])
->setCity($customer['city'])
->setCountryId($customer['country_id'])
->setPostcode($customer['postcode'])
->setTelephone($customer['phone']);
//添加送货地址和送货方式。
$order->setShippingAddress($shippingAddress)
->setShippingMethod('yourshipping_code')
->setShippingDescription('您的运输描述');
//添加您的付款方式。
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod('cod'); //cod 是货到付款
$order->setPayment($orderPayment);
//遍历报价单中的项目,创建订单项目并将其添加到订单中,您只有一个产品,所以它只会添加一个订单项目。
foreach ($quote->getItemsCollection() as $item)
{
$_prod = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
$rowTotal = $item->getCalculationPrice() * $item->getQty();
$orderItem = Mage::getModel('sales/order_item')
->setStoreId($storeId)
->setQuoteItemId(0)
->setQuoteParentItemId(NULL)
->setProductId($_prod->getId())
->setProductType($_prod->getTypeId())
->setQtyBackordered(NULL)
->setTotalQtyOrdered($item->getQty())
->setQtyOrdered($item->getQty())
->setName($_prod->getName())
->setSku($_prod->getSku())
->setPrice($item->getPrice())
->setBasePrice($item->getPrice())
->setOriginalPrice($item->getPrice())
->setRowTotal($rowTotal)
->setBaseRowTotal($rowTotal);
$order->addItem($orderItem);
}
//设置总数
$subTotal = $quote->getGrandTotal();
$order->setSubtotal($subTotal)
->setBaseSubtotal($subTotal)
->setGrandTotal($subTotal)
->setBaseGrandTotal($subTotal);
//先下订单,然后保存。
$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));
$事务->保存();
$quote->保存();
//清除会话和购物车。
Mage::getSingleton('checkout/session')->clear();
foreach($quote->getItemsCollection() as $_item)
{
Mage::getSingleton('checkout/cart')->removeItem($_item->getId())->save();
}
//将用户重定向回你想要的位置。