简体   繁体   中英

Magento 2: Unable change order status back to Processing from Complete

For debug purposes, I need to change the status of some orders back to Processing, after they've been already shipped, so the status is Complete.

I'm trying to do this programmatically, so I deleted the shipments of the order, and also the Invoices, but I cannot force the status to get back to Processing and it remains Complete.

Is it possible to do it, or once the status is complete, one cannot go back in the status flow?

Just a snippet of code:

protected function deleteShipments(){
    foreach($this->_ordersToProcess as $incrementId){
        $myOrder = $this->_order->loadByIncrementId($incrementId);

        if($this->_registry->registry('isSecureArea')){
            $this->_registry->unregister('isSecureArea');
        }
        $this->_registry->register('isSecureArea', true);

        $_shipments = $myOrder->getShipmentsCollection();

        if($_shipments){
            foreach($_shipments as $_shipment){
                $_shipment->delete();
            }
        }   

        $_invoices = $myOrder->getInvoiceCollection();

        if($_invoices){
            foreach($_invoices as $invoice){
                $invoice->delete();
            }
        }

        $myOrder->setState(\Magento\Sales\Model\Order::STATE_PROCESSING, true)->setStatus(\Magento\Sales\Model\Order::STATE_PROCESSING, true); 
        $myOrder->save();
    }

}

Order statuses cannot be changed in the admin panel, as it disrupts the business logic of Magento itself. Generally, it's only possible to cancel, hold and unhold pending orders. If you want to cancel an order with a 'Processing' status or 'Complete" status you will have to create a credit note for this.

For more info you can look here https://www.mag-manager.com/useful-articles/magento-orders-management/magento-change-order-status-to-any-from-any/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM