Magento – Cannot change order status back to Processing from Complete

magento-2.1magento2orderssales-ordershipment

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();
    }

}

Best Answer

You'll also have to reset the shipping status on the individual order items. Otherwise Magento will just reset the status right back to Complete.

foreach ($myOrder->getAllItems() as $item) {
    $item->setQtyInvoiced(0); // Not really needed but good for a full reset
    $item->setQtyShipped(0);
    $item->save();
}

Put this code before $myOrder->save();.

Related Topic