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.
Put this code before
$myOrder->save();
.