0

If I want to use this hook in order to execute a code when the order is complete.

function hook_commerce_order_update($order) {
  if ($order->status == 'completed') {
    // ...
  }
}

Is this condition sufficient, or should I add the following in order to prevent the confusion with the complete status that belongs to the checkout state?

 if ($order->status != $order->original->status) {
    $status = commerce_order_status_load($order->status);
    $state = commerce_order_state_load($status['state']);
    if (($state['name'] == 'completed') && ($order->status == 'completed')) {
      // ... 
    }
  }

1 Answer 1

0

All the checkout order statuses are prefixed with checkout_, so there is no need to check the state. Just bear in mind that in Commerce 1.x, an order may be moved into and out of that status multiple times. If you need to ensure an operation only occurs once per order, you can follow the core precedent of logging the timestamp for your operation in the $order->data array.

2
  • 1
    thank youuu !!!
    – Jasmin
    Commented Nov 10, 2020 at 16:42
  • @Jasmin My pleasure! It would be helpful if you could mark this answer as the correct answer and upvote it. Thanks! : ) Commented Nov 11, 2020 at 20:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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