我正在创建自动结帐功能,它会立即创建几个订单:
这有什么问题?
这个脚本假设为不同的用户创建了4个不同订单的不同订单,但问题在于,第一个订单后创建的每个订单都有第一个订单的小计和总计。我该如何重置?
法师::应用( '默认');
/**
* 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 = '';
}
答案 0 :(得分:2)
问题得到解决:
在结账/会话以及必须清算的客户/会话中撒谎。
Mage::getSingleton('checkout/session')->clear();
Mage::getSingleton('customer/session')->clear();
这可能有助于某人在使用类似方法解决批量订购解决方案时。
谢谢, 亚当