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.

1
+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

2
  • So, saving the new state will fire off any state-change events, hooks, etc? – vintorg Aug 30 '20 at 13:08
  • 1
    @vintorg you bet 😉 – No Sssweat Aug 30 '20 at 13:22
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 '20 at 2:22
  • Just add this to your .twig template where you want the button to appear – GiorgosK Aug 29 '20 at 6:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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