TransWikia.com

How to update the lineItemStatus of an order on the front end with twig?

Craft CMS Asked by MetaMachine on June 1, 2021

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.

One Answer

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

Correct answer by James Smith on June 1, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP