如何手动将订单状态设置为“完成”。
我正在使用以下代码,但它给出的错误说, 订单状态'完成'不得手动设置。
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE);
$order->save();
答案 0 :(得分:22)
我为自己找到了解决方案,
$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
$order->setData('state', "complete");
$order->setStatus("complete");
$history = $order->addStatusHistoryComment('Order was set to Complete by our automation tool.', false);
$history->setIsCustomerNotified(false);
$order->save();
答案 1 :(得分:10)
好吧,制作订单状态COMPLETE
的实际方法是创建invoice
和shipment
,然后订单状态自动获得COMPLETE
状态。像:
//create invoice for the order
$invoice = $order->prepareInvoice()
->setTransactionId($order->getId())
->addComment("Invoice created from cron job.")
->register()
->pay();
$transaction_save = Mage::getModel('core/resource_transaction')
->addObject($invoice)
->addObject($invoice->getOrder());
$transaction_save->save();
//now create shipment
//after creation of shipment, the order auto gets status COMPLETE
$shipment = $order->prepareShipment();
if( $shipment ) {
$shipment->register();
$order->setIsInProcess(true);
$transaction_save = Mage::getModel('core/resource_transaction')
->addObject($shipment)
->addObject($shipment->getOrder())
->save();
}
答案 2 :(得分:0)
请将对象管理器更改为 di
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$orderRepository = $objectManager->create('\Magento\Sales\Api\OrderRepositoryInterface');
$order = $orderRepository->get($orderId);
$order->setState('awaiting_stock');
$order->setStatus('awaiting_stock');
$orderRepository->save($order);
答案 3 :(得分:-1)
以编程方式设置订单状态:
http://blog.chapagain.com.np/magento-how-to-change-order-status-programmatically/
change order status to 'Completed'
$orderIncrementId = YOUR_ORDER_INCREMENT_ID;
$order = Mage::getModel('sales/order')
->loadByIncrementId($orderIncrementId);
$order->setState(Mage_Sales_Model_Order::STATE_COMPLETE,true)->save();