Counting items on an order that are in a specific collection and tagging the order accordingly

I need to create a condition in Flow that counts all of the items on a new order that are in a specific collection and then tags that order with a tag such as x-cups (x being the number of items in the cups collection on the order).

So if there were 7 units of items in the cups collection the order tag would be “7-cups”.

Currently the only way you can count some lineItems is to use liquid. If you want to add a “7-cups” tag, then you can add the liquid in that tag field.

Roughly you would do something like:

{% assign the_count = 0 %)
{% for li in order.lineItems %}
{% for collection in li.product.collections %}
{% if collection.title == "your title %}
{% assign the_count = the_count | plus: 1 %}
{% endif %}{% endfor %}{% endfor %}
{{ the_count }}_cups

I used your script on this example order

Bag of Mint Varnish cups, quantity 2

Bag of S’mores Varnish cups quantity 1

Bag of Strawberry Varnish cups quantity 3

where all 3 of these items were in the “wonderful-Varnish-Cups” collection.

it returned the tag of 3-cups but I was expecting 6-cups which is the total line quantity of bags for items in the “wonderful-Varnish-Cups” colllection. Any idea why? Below is the exact script:

{% assign the_count = 0 %}

{% for li in order.lineItems %}

{% for collection in li.product.collections %}

{% if collection.title == “wonderful-Varnish-Cups” %}

{% assign the_count = the_count | plus: 1 %}

{% endif %}{% endfor %}{% endfor %}

{{ the_count }}_cups

I did more testing and it is not capturing the line quantities.

That script is counting the lineItems that match and not their quantity. I would change the second assign to:

{% assign the_count = li.quantity | plus: the_count %}
1 Like

I tried this but I am getting “0-cups” for all of the orders…

{% assign total_item_count = 0 %}

{% for li in order.lineItems %}

{% for collection in li.product.collections %}

{% if collection.title == “wonderful-Varnish-Cups” %}

{% assign item_quantity = order.lineItems.quantity | times: 1 %}

{% assign total_item_count = total_item_count | plus: item_quantity %}

{% endif %}{% endfor %}{% endfor %}

{{ total_item_count }}_cups

you can’t use order.lineItems directly like that. You need to use li

Hi @paul_n I am trying to do the same, but where do I put that liquid? I can’t find such action in Shopify flow

Right in the tag field. Don’t forget to push enter.

Pro tip- save the code in the description in case you need to edit it later.

where one should write such script?

i don’t follow the question..what do you mean by where?

you suggested to use such script:

{% assign the_count = li.quantity | plus: the_count %}

where one suppose to use that, in liquid?

In the tag field you can add liquid. Make sure to hit enter.