How to programmatically change order status


Changing the order state in Drupal Commerce 2.x like this:

$order->set('state', 'canceled');

is not a good idea because you are not dispatching transition events. And some modules depend on those events. The proper way to change the order state is to apply state transition like this:

$order = \Drupal::entityTypeManager()

You can find the list of transition IDs in the workflows file.