3

I'm running Commerce Marketplace and provide store owners a back-end portal for managing their store.

In their portal, they can view orders sitting in the "fulfillment" state with the shipping address. Once they fulfill that order, I need to provide a button for them to move that order to the "completed" state.

How do I move an order to the "completed" state? Do I send the user to a specific checkout route, or can I move the order itself to completed via the API?

Currently, the only way to move the order to "completed" is manually in the order under the Commerce administration.

2 Answers 2

2
+50

can I move the order itself to completed via the API?

Create button and create a new route. In that route's class you can do something like this. Example if order number is 1:

  $entity_manager = \Drupal::entityTypeManager();
  $entity_storage = $entity_manager->getStorage('commerce_order');
  $order = $entity_storage->load(1); // load order 1
  $current_state = $order->getState()->value;
  if ($current_state == 'fulfillment') {
    $order->set('state', 'completed');
    $order->save();
  }

OR alternatively if they need to be able to do this for several orders at the same time. (Checkmark several, and apply to selected items). Instead of a button & route, create a custom View Bulk Operation action and use same code as above, Ex:

enter image description here

3
  • So, saving the new state will fire off any state-change events, hooks, etc?
    – vintorg
    Aug 30, 2020 at 13:08
  • 1
    @vintorg you bet 😉
    – No Sssweat
    Aug 30, 2020 at 13:22
  • @NoSssweat I am not getting the order to complete after applying the transition. For eg: after $order->getState()->applyTransitionById('fulfill'); I have to make the order cart to 0 to make the order to get complete. ie, $order->unlock(); $order->setPlacedTime(\Drupal::time()->getCurrentTime()); $order->setOrderNumber($orderId); $order->set('cart', 0); $order->set('state', 'completed'); $order->save();. Any idea?
    – KTM
    Oct 20 at 5:40
1

Perhaps one way to do this would be similar to what you can find in modules/contrib/commerce/modules/order/templates/commerce-order--admin.html.twig

{# If the order has possible transitions, render the field for transition buttons. #}
{% if order_entity.getState.getTransitions is not empty %}
<div class="entity-meta__header">
  {{ order.state }}
</div>
{% endif %}

If the order is configured to pass from state fullfilled to completed the appropriate button for moving state should be available there. Make sure your order_entity is passed to the twig template.

Read more about order workflow states

2
  • I'm not following. Should I redirect to a route?
    – vintorg
    Aug 29, 2020 at 2:22
  • Just add this to your .twig template where you want the button to appear
    – GiorgosK
    Aug 29, 2020 at 6:01

Your Answer

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

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