8

如何手动将订单状态设置为“完成”。

我正在使用以下代码,但它给出的错误 是,不能手动设置订单状态“完成”。

$order = Mage::getModel('sales/order')->loadByIncrementId($order_id);
        $order->setState(Mage_Sales_Model_Order::STATE_COMPLETE);
        $order->save(); 
4

4 回答 4

24

我为自己找到了解决方案,

$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();
于 2013-09-10T05:30:21.500 回答
11

好吧,创建订单状态的实际方法COMPLETE是创建invoiceand 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();
}
于 2013-09-10T05:36:45.327 回答
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);
于 2021-06-24T13:37:38.480 回答
-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();
于 2013-09-10T09:34:17.077 回答