1

I'm building a simplified interface that is only going to be used by employees preparing the orders. They are in a usergroup that has the right permissions to update orders. I'm building this because updating an 'order status' or 'line item status' from the controlpanel is too finicky to do from a smartphone or tablet.

I've found how to update an order status here and got that working nicely.

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

What I don't know how to do is how to update the lineItemStatus. I'll probably have to fetch the order first then the lineItem id and then update the lineItemStatus attribute, but I have no idea how to even start.

Any help putting me in the right direction is greatly appreciated!

*I also know this would be better to do with a module or plugin, but alas, I don't write php.

2

You can use the same technique to set the line item data like this:

{% set order = craft.orders.id(123).one() %}

{% for lineItem in order.lineItems %}
    {% do lineItem.setAttributes({lineItemStatusId: 2}, false) %}
{% endfor %}

{% do order.setAttributes({
    orderStatusId: 4,
    lineItems: order.lineItems
}, false) %}

{% do craft.app.getElements().saveElement(order) %}

^ That would set all line items in an order to the same status... I imagine you have some kind of interface where you can either update a single line item at a time or mark several at once and press a submit button, so in that scenario you'll probably want to add some conditionals inside the loop to check that the line item id you're on matches say, the line item id passed in through the URL or through POST data or whatever so that just the right one(s) get updated

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.