1
我創建自動結賬功能,這將創建一次幾個訂單:自動onepage結帳 - 報價問題
這是什麼問題?
這個腳本讓我們假設爲不同的用戶創建4個不同的產品的訂單,但是問題在於這裏,第一個訂單後創建的每個訂單都有第一個訂單的小計和總計。我該如何重置? Mage :: app('default');其中,Mage :: app('default');其中Mage :: app('default');其中,
/**
* Get the resource model
*/
$resource = Mage::getSingleton('core/resource');
/**
* Retrieve the read connection
*/
$readConnection = $resource->getConnection('core_read');
$query = 'SELECT * FROM m4gsrepeated_orders WHERE execution <= CURDATE()';
$all_orders = $readConnection->fetchAll($query);
function addCartItems($products_array) {
$cart = Mage::getModel('checkout/cart');
foreach($products_array as $productw) {
$sku = $productw[0];
/** @var $productCollection Mage_Catalog_Model_Resource_Product_Collection */
$productCollection = Mage::getModel('catalog/product')
->getResourceCollection()
->addFieldToFilter('sku', $sku);
/** @var $product Mage_Catalog_Model_Product */
$product = $productCollection->getFirstItem();
// you should have the product now, maybe it isn't even necessary to get the ID
$product = $product->load($product->getId());
$cart->addProduct($product, $productw[1]);
}
$cart->save();
}
foreach ($all_orders as $order) {
//Set basic data
Mage::getSingleton('checkout/session')->getQuote()->setReservedOrderId(null);
$customer = Mage::getModel('customer/customer')
->setWebsiteId(Mage::app()->getStore()->getWebsiteId())
->load($order['user_id']);
// Set as Logged In and Clear Session
Mage::getSingleton('customer/session')->setCustomerAsLoggedIn($customer)->renewSession();
//Set up cart
$cart = Mage::getModel('checkout/cart')->getQuote();
//get current cart items;s
$i=1;
foreach ($cart->getAllItems() as $item) {
$products_current[$i][0] = $item->getProduct()->getSku();
$products_current[$i][1] = intval($item->getQty());
$i++;
}
$i=1;
$cart = Mage::getModel('checkout/cart');
foreach(Mage::getSingleton('checkout/session')->getQuote()->getItemsCollection() as $item){
$cart->removeItem($item->getId());
}
$products_delayed = json_decode($order['items']);
addCartItems($products_delayed);
$cart->save();
//LUUUTA CREATE ORDER
# Get customer default shipping information
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultShipping();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
$shippingAddress = $address->getData();
}
# Get customer default billing information
$customerAddressId = Mage::getSingleton('customer/session')->getCustomer()->getDefaultBilling();
if ($customerAddressId){
$address = Mage::getModel('customer/address')->load($customerAddressId);
$billingAddress = $address->getData();
}
$quote = Mage::getSingleton('checkout/session')->getQuote();
/*
* One page checkout consists of following steps
* (1) Customer login method aka Checkout method
* (2) Billing information (address)
* (3) Shipping information (address)
* (4) Shipping method
* (5) Payment information
* (6) Order review, in short: DO THE ORDER
*/
$storeId = Mage::app()->getStore()->getId();
$checkout = Mage::getSingleton('checkout/type_onepage');
$checkout->initCheckout();
$checkout->saveShippingMethod('excellence_excellence');
$quote->getShippingAddress()->setShippingMethod('excellence_excellence');
$quote->getShippingAddress()->unsGrandTotal(); //clear the values so they won't take part in calculating the totals
$quote->getShippingAddress()->unsBaseGrandTotal();
$quote->getShippingAddress()->setCollectShippingRates(true)->save();
$quote->getShippingAddress()->collectTotals(); //collect totals and ensure the initialization of the shipping methods
$quote->collectTotals();
//STEP(1)
$checkout->saveCheckoutMethod('register');
//STEP(2)
$checkout->saveBilling($billingAddress, false);
//STEP(3)
$checkout->saveShipping($shippingAddress, false);
//STEP(4)
$checkout->saveShippingMethod('excellence_excellence');
//STEP(5)
$checkout->savePayment(array('method'=>'pay'));
Mage::getSingleton('checkout/type_onepage')->getQuote()->getShippingAddress()->setShippingMethod('excellence_excellence');
//STEP(6)
/*
* $checkout->saveOrder() returns array holding empty object
* of type Mage_Checkout_Model_Type_Onepage
*/
try {
$checkout->saveOrder();
}
catch (Exception $ex) {
echo $ex->getMessage();
}
//addCartItems($products_delayed);
$cart->truncate();
$cart->save();
Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
Mage::getSingleton('customer/session')->logout();
$customerAddressId = '';
}