我會繼續遵循Magento邏輯,而不會破壞它的正常流程。來自我之前完成的腳本的代碼片段描述了來賓客戶的完整結賬流程。你的應該不會有太大的不同。請不要複製和粘貼,查看代碼並根據需要調整代碼。
//Load the product from the posted form and cart and quote models.
$product = Mage::getModel('catalog/product')->load((int) $_POST['product_id']);
$cart = Mage::getSingleton('checkout/cart');
$quote = $cart->getQuote();
//Add the product to the cart and set the session to updated
$params = array('product' => $product->getId(), 'qty' => (int) $_POST['qty']);
$cart->addProduct($product, $params);
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
//Load variables needed for the order process and transaction object to save the order.
$transaction = Mage::getModel('core/resource_transaction');
$storeId = $customer->getStoreId();
$reservedOrderId = Mage::getSingleton('eav/config')->getEntityType('order')->fetchNewIncrementId($storeId);
//Load the order model and set some default values
$order = Mage::getModel('sales/order')
->setIncrementId($reservedOrderId)
->setStoreId($storeId)
->setQuoteId($quote->getId())
->setGlobalCurrencyCode('USD')
->setBaseCurrencyCode('USD')
->setStoreCurrencyCode('USD')
->setOrderCurrencyCode('USD');
//Assign customer as guest, $customer is array from $_POST['customer']
$order->setCustomerEmail($customer['email'])
->setCustomerFirstname($customer['firstname'])
->setCustomerLastname($customer['lastname'])
->setCustomerGroupId(0)
->setCustomerIsGuest(1);
//Prepare billing address and add it to the order
$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);
//Prepare shipping address and add it to the order, then set the shipping method
$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']);
//Add the shipping address and the shipping method.
$order->setShippingAddress($shippingAddress)
->setShippingMethod('yourshipping_code')
->setShippingDescription('Your shipping description');
//Add your payment method.
$orderPayment = Mage::getModel('sales/order_payment')
->setStoreId($storeId)
->setCustomerPaymentId(0)
->setMethod('cod'); //cod is Cash on Delivery
$order->setPayment($orderPayment);
//Loop through the items in the quote, create order item and add it to the order, you have only one product, so it will add only one order item.
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);
}
//Set the totals
$subTotal = $quote->getGrandTotal();
$order->setSubtotal($subTotal)
->setBaseSubtotal($subTotal)
->setGrandTotal($subTotal)
->setBaseGrandTotal($subTotal);
//First, place the order, then save it.
$transaction->addObject($order);
$transaction->addCommitCallback(array($order, 'place'));
$transaction->addCommitCallback(array($order, 'save'));
$transaction->save();
$quote->save();
//Clear the session and the cart.
Mage::getSingleton('checkout/session')->clear();
foreach($quote->getItemsCollection() as $_item)
{
Mage::getSingleton('checkout/cart')->removeItem($_item->getId())->save();
}
//Redirect the user back to where you want.