I am looking for a way to change the status of some orders. I need to be able to cancel orders even if the payment has been validated. In fact, I'm currently using the Commerce Payplug module which places transactions while the Payplug IPN says the transaction was not accepted.
I don't know yet if I'm going to try to fix this module or if I'm going to look for another solution to receive payments but, in the meantime, I need to be able to cancel the corresponding orders.
If with Drupal Commerce 1 this operation did not pose any problem, it seems that in version 2 it is more problematic. The user interface does not seem to natively allow any modification of orders status.
I tried to install the Commerce Order Status Update module, but the installation fails with a WSOD. Then, I tried to programmatically change the status.
$order = \Drupal\commerce_order\Entity\Order::load($form['id_order']["#value"]);
$order_state = $order->getState();
$order_state->applyTransitionById('cancel');
$order->save();
The code throws this exception.
InvalidArgumentException: The transition "cancel" is currently not allowed. (Current state: "completed".) in Drupal\state_machine\Plugin\Field\FieldType\StateItem->applyTransition() (line 329 of modules/contrib/state_machine/src/Plugin/Field/FieldType/StateItem.php).
What is the solution to cancel an order?