1

Following on from this question Commerce: How to update an order status via the frontend

We'd like to make it easy for delivery drivers to update the status of a craft commerce order, from a frontend page. Doing it via the CP on mobile is a bit cumbersome as it involves multiple steps.

We've created a user group call couriers.

{% extends '_layouts/_logistics' %}

{% block logistics %}
    {% if currentUser and currentUser.isInGroup('courier') %}
        {% include 'logistics/_orders' %}
    {% else %}
        {% redirect 'account' %}
    {% endif %}
{% endblock %}

In the _orders include we'd like to show a list of orders, each with a 'Mark as Delivered' button, which can be clicked as the orders are fullfilled.

<dl>
    {% if order.shippingAddress %}
        {% include 'shop/_includes/addresses/address' with { address: order.shippingAddress } %}
    {% endif %}
    <dt>{{ order.reference }}</dt>
</dl>
<form method="POST">
    <input type="hidden" name="action" value="">
    {{ redirectInput('logistics') }}
    {{ csrfInput() }}
    <input type="hidden" name="orderStatusId" value="4">
    <input class="button button--primary" type="submit" value='Mark as Delivered'>
</form>

I'm wondering if this can be done, without the need to build a plugin or module? Else is there a plan to include the ability to do this in a future Craft Commerce release.

Many thanks,

Andrew

1

This feels a bit dirty, but you can actually do this directly in a Twig template like this:

{% set order = craft.orders.id(123).one() %}
{% do order.setAttributes({orderStatusId: 4}, false) %}
{% do craft.app.getElements().saveElement(order) %}

-- naturally you'd need a way to fetch which order you want to update, but you could easily grab that from your POST data or from the URL or localStorage or whatever.

1
  • This is just what I was looking for. Obviously better to use a controller, however for our purposes this will work. Many thanks for providing a solution.
    – Andrew F
    Sep 2 '20 at 14:50

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.