1

In some (not all) cases, I want to set the status of an order to a new custom status after it has been shipped.

Where in the core code is an order updated to complete once it has been invoiced and shipped?

1

You can create a new order status by going to Stores > Configuration > Order Status, and create a new status for that and make sure it is enabled and you'll be able to see your custom generated status under order edit settings.

enter image description here

Now to set your order to custom status whenever it is marked as shipped, you can put an observer event sales_order_save_after, which will execute whenever there is a change in order data, so you need to check the order status in Observer event and if it is changed to shipped code you can override it into your observer execution to your custom status code programmatically.

I hope this will help you. If not, feel free to comment.

1
  • I knew how to create the new order status, but that does help make it a complete answer. I hadn't considered using an observer to achieve this, I think I was overcomplicating things too much! – Geat Jun 29 '20 at 15:13
0

It turns out using an observer wasn't the optimal solution - another process further down the chain was setting the status back to complete. I also didn't feel that overwriting a previously set status was the most elegant way to achieve what I needed.

I was able to find the method responsible for changing the status of an order after it's set to both shipped and invoiced:

\Magento\Sales\Model\ResourceModel\Order\Handler\State::check($order)

This is called whenever an order's status changes (and in all likelihood is what was reverting the status change from my observer)

By creating an after plugin, I was able to change the status as part of the normal order workflow.

public function afterCheck($subject, $result, $order)
{
  if ($my_logic) {
    $order->setState(Order::STATE_PROCESSING)
      ->setStatus('my_status');
    }
}
0

It's better to create it via the admin panel by going to Stores > Configuration > Order Status, and ensure to create a new status for it. Don't forget to enable it. I Hope, with this you can see your custom generated status under order edit settings.

1
  • Yes, I created the order status through the admin system first. I just needed to assign it at certain points in the workflow. – Geat Jun 30 '20 at 15:04

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.