How can I hide a product based on a metafield date comparison?

Solved

How can I hide a product based on a metafield date comparison?

Troyn
Shopify Partner
7 1 2

Hey, 

I'm currently trying to hide a product with the workflow below, all of it works apart from the date comparison.

 

The metafields_item.value that I'm trying to compare is a metafield stored as a date. When I sent metafields_item.value to my email to check the value it returned a string YYYY-MM-DD so i've tried to compare it too {{ "now" | date: "%Y-%m-%d" }} this doesn't work I've also  tried to compare it to {{ "now" | date: "%Y-%m-%d" | date: '%s' }} this also didn't work. 

 

Have also tried if value is equal to and value includes.

 

Can any one see what is wrong with my comparison?

Screen Shot 2023-10-11 at 11.09.20 AM.png

Accepted Solution (1)
Troyn
Shopify Partner
7 1 2

This is an accepted solution.

Updated to one workflow.

Troyn_1-1698637448644.png

 

Get Product (1) and (2) use query is checking for any product updated in the future.  updated_at:>'{{ scheduledAt }}'

 

{% assign today_date = 'now' | date: "%Y-%m-%d"%}
{% for metafields_item in getProductDataForeachitem.metafields %}
{% if metafields_item.key == 'product_expiry_date' %}
{% assign expiry_date = metafields_item.value | date: "%Y-%m-%d" %}
{% if today_date == expiry_date %}
product_expired,
{% endif %}
{% endif %}
{% endfor %}

View solution in original post

Replies 28 (28)

Steve_L
Shopify Staff
11 2 3

Hi,

 

Flow consumes and exposes the values from the shopify graphql admin api, and since the metafield value field is a string, it is treated as a string in Flow rather than a date. This is something we're investigating if we can add better support for, but currently isn't possible within Flow conditions.

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

Troyn
Shopify Partner
7 1 2

Hi Steve, Thanks for the response. I did some further testing and have one more question. 

 

I did some testing where I compared {% assign date_today = "now" | date: "%Y-%m-%d" | append: '' %}{{date_today}} to the string value "2023-10-17" and it returned true (testing for this was done in the CMS on the product page if that maters). Then I compared the metafield in Shopify flow to the string value "2023-10-17" it also returns true. However when I compare the metafield to {% assign date_today = "now" | date: "%Y-%m-%d" | append: '' %}{{date_today}} in Shopify flow it returns false. Which in theory should  have converted the variable "date_today" to a string in the correct formatting as we can see from my first test.

 

So in Shopify flow {% assign date_today = "now" | date: "%Y-%m-%d" | append: '' %}{{date_today}} doesn't assign  the variable "date_today" to a string? Sorry it's just not very clear why the comparison is invalid. 

paul_n
Shopify Staff
1313 148 299

I would recommend outputting the values using "Log output". I'd suspect one of the values is not what you expect. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
Troyn
Shopify Partner
7 1 2

Hey, thanks I didn't know about the "Log output". I found out what the issue was, the code logic is correct. However the issue is that I can't inject liquid code in to the value field, is there any way I can use liquid code in the value field?Screenshot 2023-10-24 at 09.47.14.png

paul_n
Shopify Staff
1313 148 299

You cannot use liquid in conditions currently. 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
FePixie
Shopify Partner
30 2 7

I am having the exact same issue as Troyn

how are you supposed to check if a date metafield is older than today in flow?

the available conditions also dont offer less than or greater than - only equal to 😕

any suggestions please?

 

paul_n
Shopify Staff
1313 148 299

You can only do that where liquid is available, which means you need to use it in an action. That may or may not work depending on your exact use case.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
FePixie
Shopify Partner
30 2 7

how about in the get order data part?

can you do something like this there?

i havent got it right obviously...

 

I am trying to find orders where the metafield custom.delivery_date has passed

Capture.JPG

 

 

Troyn
Shopify Partner
7 1 2

This is what I'm currently looking into as well @FePixie, I'll let you know if I get any where with this approach.

FePixie
Shopify Partner
30 2 7

I'm still not getting anywhere with flow 😞

i can do it with the admin REST api - but its such a cumbersome thing by the time you paginate requests and rate limit everything to a crawl because you have to ask so many times 😕  never mind having to host it elsewhere...

 

Do you have any suggestions for us @paul_n ?

I am trying to find orders where the metafield custom.delivery_date has passed

and @Troyn is trying to find products where the metafield product_expiry_date date has passed

FePixie
Shopify Partner
30 2 7

hmm - i thought i might try checking externally - but a POST or GET does not seem to give me any output reply i can check?  not even a response code?

FePixie_0-1698204602293.png

on this page it looks like it should - am i missing something?

https://help.shopify.com/en/manual/shopify-flow/reference/actions/send-http-request

FePixie_1-1698204833899.png

 

 

paul_n
Shopify Staff
1313 148 299

You need to build a task currently if you want return data (that feature was added to the CLI in summer editions)

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
FePixie
Shopify Partner
30 2 7

Can you aim me at some docs about building a 'task' please?  Not sure what you mean by that?

paul_n
Shopify Staff
1313 148 299

Docs on creating an action. https://shopify.dev/docs/apps/flow/actions

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
paul_n
Shopify Staff
1313 148 299

You cannot query by metafields yet - it's a limit of the API but would obviously be very useful for this kind of use case. 

 

You could potentially do something like:
Order created -> Add order tag (with MF value...deliver_sep_21)

 

Then a second workflow:

  • Every day
  • Get order data (query something like tag:deliver_sep_21). See docs for how to do relative dates 
  • Then do whatever action you want.

 

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
FePixie
Shopify Partner
30 2 7

Oh poop - That's gonna end up with a bunch of funny messy tags - and will make it very hard for my customer to filter anything in admin to see which ones need dates manually added.  I'm reading a supplier spreadsheet via a script to add them to the metas with the rest api - i suppose i'm gonna need to write my own script for this part too  😕

can you add searching by metafields and liquid in filters to the flow wishlist please!

 

paul_n
Shopify Staff
1313 148 299

Searching by metafields isn't a Flow request - it's a request for the metafields people. Once it's there, you should be able to use it automatically in Flow.

 

But noted about liquid in conditions. We are looking at how to enable that (better support for code overall)

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
FePixie
Shopify Partner
30 2 7

This seems to work @Troyn ...

FePixie_0-1698374313726.png

putting code in the action as Paul said

here's my code in the add tag bit...

{% assign today_date = 'now' | date: '%s' %}
{% for metafields_item in getOrderDataForeachitem.metafields %}  
{% if metafields_item.key == 'delivery_date' %}
{% assign pre_date = metafields_item.value | date: '%s' %}
{% if today_date > pre_date %}
OLDDATE,
{% else %}
Has ETA,
{% endif %}
{% endif %}
{% endfor %}

i hope something like that works for you on the products too 🙂

FePixie
Shopify Partner
30 2 7

small refinement - i chucked out all the OLDDATE tags before going through and re-tagging - to remove the ones where the date has been changed.

FePixie_0-1698379977380.png

i hope that saves someone the heap of hours i spent on it 🙂

Troyn
Shopify Partner
7 1 2

Thanks for all the help @FePixie and @paul_n!

 

I was able to get it working. I ended up using 3 workflows. Here's a rundown if any one needs to do something similar in the future. 

 

1) add a tag on a product if the metafield is equal to todays date runs at 00:00

Troyn_0-1698632863373.png

{% assign today_date = 'now' | date: "%Y-%m-%d"%}
{% for metafields_item in getProductDataForeachitem.metafields %}
{% if metafields_item.key == 'product_expiry_date' %}
{% assign expiry_date = metafields_item.value | date: "%Y-%m-%d" %}
{% if today_date == expiry_date %}
product_expired,
{% endif %}
{% endif %}
{% endfor %}

 

2) archive the product with the added tag runs at 00:01

Troyn_1-1698633093539.png

 

3) remove that tag from the product (allows for the product to be reactivated without workflow 2 archiving it again)

Troyn_2-1698633155725.png

 

FePixie
Shopify Partner
30 2 7

nice one 🙂

did it not play nice if you add the next flow under the 'Then' on the foreach step?

Troyn
Shopify Partner
7 1 2
  • Click Then to add a single action or a condition. This action is taken after all the actions performed by Do this for each item are complete.

I didn't read the documentation completely so I missed the fact that the action occurs after each action. I'll correct the workflow now.

FePixie
Shopify Partner
30 2 7

that'll help if the shopify end goes slow - i've seen it take more than 1 min when it's got a bellyache 👍

Troyn
Shopify Partner
7 1 2

This is an accepted solution.

Updated to one workflow.

Troyn_1-1698637448644.png

 

Get Product (1) and (2) use query is checking for any product updated in the future.  updated_at:>'{{ scheduledAt }}'

 

{% assign today_date = 'now' | date: "%Y-%m-%d"%}
{% for metafields_item in getProductDataForeachitem.metafields %}
{% if metafields_item.key == 'product_expiry_date' %}
{% assign expiry_date = metafields_item.value | date: "%Y-%m-%d" %}
{% if today_date == expiry_date %}
product_expired,
{% endif %}
{% endif %}
{% endfor %}

kwat1
Visitor
1 0 1

This is great!

 

I believe this can be further simplified in the second column (get product data (1)) and the query can be set to updated_at:>'{{ scheduledAt }}' tag:"product_expired" and third column (get product data (2)) can be set to updated_at:>'{{ scheduledAt }}' tag:"product_expired" status:"archived"

KenChow
Shopify Partner
1 0 0

hello, I am trying to follow the steps you are creating but when I try to run, it seems it is running but very slow and the end it disappear in my "recent runs"... i am not sure why.. and here is the steps

2024-04-18 191832.png

 

 

also, i would like to ask which options for GetProductData is correct
2024-04-18 191608.png

 

Both have Key and value? thanks for your help! 😀
paul_n
Shopify Staff
1313 148 299

Do not use metafieldDefinitions. They are the schema for your metafields, not the metafields. 

 

Do you have any successful or failed workflow runs? If so, please share one that didn't work as expected. If no, then you likely have an extreme inefficiency in the workflow somewhere. If that's true, post a sceenshot of your condition and any liquid code.

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.
paul_n
Shopify Staff
1313 148 299

Flow just launched improved metafields support, which makes it much easier to access and use metafield data in Flow. 

 

https://changelog.shopify.com/posts/shopify-flow-access-typed-metafields

Paul_N | Flow Product Manager @ Shopify
- Finding Flow useful? Leave us a review
- Need Flow help? Check out our help docs.
- Building for Flow? Check out Flow's dev docs.