1

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

}
1

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

  • um... tried but the status remains 'complete'. Actually you reminded me I had a similar but complementary problem: after creating the shipment, status was not passing from processing to complete, and I had to save the order at each item and in the end it is working. On this side no, but good try. – sissy Feb 7 '18 at 14:23
  • That's strange. Are you sure there's nothing that hooks into sales_order_save_after perhaps? I used your exact code and after adding the item save code it worked. This was on 2.1.9 btw (since you tagged it with magento 2.1) – sduif Feb 7 '18 at 14:33
  • I'm on M2.1.10 so the version is similar. Don't think there's something changing the state after save... Maybe it's because i've already deleted the shipment so no items are found? I will try to re-create the shipment and then delete everything together. – sissy Feb 7 '18 at 14:36
0

Use the below code

$orderState = Order::STATE_PROCESSING;
$myOrder->setState($orderState)->setStatus(Order::STATE_PROCESSING);
$myOrder->save();

instead of

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

It may workout to you.

  • Nope, i already tried that way and it's actually the same value... no way. but thank you – sissy Feb 7 '18 at 15:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.