Have your say in Community Polls: What was/is your greatest motivation to start your own business?

Flows Get Order query that can fetch an order only if it contains a specific tag?

Solved

Flows Get Order query that can fetch an order only if it contains a specific tag?

tylerjlang
Visitor
2 0 0

At my company, we want to tag customers with a tag called "Premium" based on if they have had 3+ orders of tag "Recurring Order" in last 90 days. When I use the flow, I have the following set up in mind:

Trigger "Order is Created" >> Get Order Data (Query that filters for customer of the original order, within last 90 days, and filters orders ONLY with "Recurring Order" tag) >> Count >> Conditional "if count >=3" >> Tag customer

 

The part I cannot figure out is the Liquid code to create the part of the query to filter for orders with a specific tag. I tried the following

----------------------------

customer_id:{{order.customer.legacyResourceId}} AND created_at:>='{{ "now" | date_minus:"90 day" }}' AND

order.tags contains 'Recurring Order'

-------------------------------

However, it would not fire correctly, and my thought is because, when I look at the liquid documentation, the order.tags is stored as an array, not a string. Furthermore, when I go to templates, order.tags is always used in a for loop, such as 


{% for tags_item in order.tags %}
{{tags_item}}

{% endfor %}

 

How could I use this for loop within a logic statement to check if ONLY one tag is equal to my 'Recurring Order' tag? 

 

Or would it be possible to change order.tags into a string somehow? Like '{{order.tags}}' and use a "contains" condition? 

 

 

This is my first time using liquid, so any help would be greatly, greatly appreciated! 

Accepted Solution (1)

TimothyWillard
Shopify Staff (Retired)
3 2 2

This is an accepted solution.

There are a couple of things going on here. While that field *is* a liquid string, what you are actually trying to generate from that liquid is (as mentioned in the task) a valid Shopify Query Search Syntax string. So the text that you write in the box will be parsed by liquid to replace everything inside `{{ }}` with the actual value, but then that generated text is something you want to be in the query search syntax (which is more or less entirely unrelated to liquid). 

If we look at the query parameter on the graphql `orders` search (which is what Flow ultimately uses), we can see that `tag` is a supported field. So, using a `Field search` from the Shopify Query Search Syntax (which is what you have already used for `customer_id` and `created_at`), you should be able to just add `tag:'Recurring Order'` to your query to get what you want. 

To learn more visit the Shopify Help Center or the Community Blog.

View solution in original post

Replies 5 (5)

TimothyWillard
Shopify Staff (Retired)
3 2 2

This is an accepted solution.

There are a couple of things going on here. While that field *is* a liquid string, what you are actually trying to generate from that liquid is (as mentioned in the task) a valid Shopify Query Search Syntax string. So the text that you write in the box will be parsed by liquid to replace everything inside `{{ }}` with the actual value, but then that generated text is something you want to be in the query search syntax (which is more or less entirely unrelated to liquid). 

If we look at the query parameter on the graphql `orders` search (which is what Flow ultimately uses), we can see that `tag` is a supported field. So, using a `Field search` from the Shopify Query Search Syntax (which is what you have already used for `customer_id` and `created_at`), you should be able to just add `tag:'Recurring Order'` to your query to get what you want. 

To learn more visit the Shopify Help Center or the Community Blog.

tylerjlang
Visitor
2 0 0

Oh that's amazing, thank you for these clarifications! So just to be clear, even if the order has multiple tags, tag:'Recurring Order' will be considered True if any of the many tags the customer has are equal to 'Recurring Order'?

TimothyWillard
Shopify Staff (Retired)
3 2 2

Yes, that is my understanding of the field. 

To learn more visit the Shopify Help Center or the Community Blog.

Digico
Shopify Partner
47 1 5

Tags can be parsed as an array, and Liquid can parse it. So with "contains" in Liquid, you have quite a solution.

Digico
Shopify Partner
47 1 5

Usually product, or order tags can overwrite themselves. But given the unique ID of each order, this is not an issue. It depends the context of where you ask for several services or Id of Shopify 😉