Score:2

How do I cancel an order?

br flag

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?

Giuseppe avatar
br flag
The interface allows to change order status, you need to go to the "View" order mode as admin. I dont have the code right now, but according the documentation https://docs.drupalcommerce.org/commerce2/developer-guide/orders/workflows/workflows transitions you should move the order to "fulfilment" before cancelling it. Be aware that probably will send the email etc.
Alexdezark avatar
br flag
It doesn't solve the problem. Your link doesn't work; https://docs.drupalcommerce.org/commerce2/developer-guide/orders/workflows/choosing-workflow is a better link. Indeed, independently from the choose workflow, once the order is in the "completed" state, there is no way in the user interface to cancel it, nor to return to the "Fulfillment" state. ". No more than programmatically for that matter.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.