所以,这是我的问题:如果客户下了一个新订单,并且他/她有一个现有的待处理订单,其中包含一个延期交货的项目,那么这两个订单必须合并。所以我在活动中创建了一个观察者sales_model_service_quote_submit_before
。这个想法是,在这个观察者中,我检查客户的电子邮件并检查他/她是否有一个现有的挂单。如果存在这样的订单,我将旧订单的项目放入新订单并删除/取消旧订单。这是我的功能:
public function salesQuoteSubmitBefore($observer){
$customerEmail = $observer->getQuote()->getData('customer_email');
$customer = Mage::getModel("customer/customer");
$customer->setWebsiteId(Mage::app()->getStore());
$customer->loadByEmail($customerEmail);
$oldOrder = Mage::getResourceModel('sales/order_collection')
->addFieldToSelect('increment_id')
->addFieldToFilter('customer_id', $customer->getId())
->addFieldToFilter('state', array('in' => array('new')))
->addFieldToFilter('status', array('in' => array('pending')))
->setOrder('created_at', 'desc')
->setPageSize(1);
$oldID = $oldOrder->getData();
if($oldID[0]['increment_id'] != '' && $oldID[0]['increment_id'] > 0){
$mergeOrder = false;
$order = Mage::getModel('sales/order')->loadByIncrementId($oldID[0]['increment_id']);
foreach($order->getAllItems() as $item){
$product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
if($product->getStockItem()->getQty() < 0){
$mergeOrder = true;
}
}
if($mergeOrder){
$newOrder = $observer->getOrder();
$quote = $newOrder->getQuote();
$tmp = array();
foreach($order->getAllItems() as $item){
$tmp[] = $item->getSku();
$product = Mage::getModel('catalog/product')->load($item->getProduct()->getId());
$qty = array('qty'=>$item->getQty());
$quote->addProduct($product, new Varien_object($qty));
}
foreach($quote->getAllAddresses() as $a){
$a->unsetData('cached_items_nonnominal');
$a->unsetData('cached_items_nominal');
}
$quote->setTotalsCollectedFlag(false)->collectTotals();
$quote->save();
$newOrder->setQuote($quote)->save();
$order->setState(Mage_Sales_Model_Order::STATE_CANCELED, true)->save();
//Mage::throwException('obs: '.json_encode($newOrder->getQuote()->getData()));
}
}
}
Mage:throwExeption 打印新报价,一切都正确,数量,总数......如果我打印 newOrder,一切都正确!但是下订单时使用旧数据,因此新订单没有旧项目。
什么或在哪里保存更改的订单/报价,以便将新数据用于创建订单?谢谢您的帮助!