Tag all products based on metafield date

Tag all products based on metafield date

benalexanderjon
Tourist
10 0 3

I have an online bookshop where we would like to announce when a book has just been published. Each book (product) has a custom metafield which describes the book's publication date - custom.book_publication_date. It has the format YYYY-MM-DD.

 

I would like to create a Shopify flow script whereby:

 

- all products are looked at each day

- if the custom.book_publication_date is equal to today then tag the product "published today".

- if the custom.book_publication_date is not equal to today then remove the "published today" tag.

- if the custom.book_publication_date minus today is equal to or less than 7 then tag the product "published this week".

- if the custom.book_publication_date minus today is greater than 7 then remove the "published this week" tag.

 

I will then create a smart collection based on these tags.

Can anyone help please?

Replies 6 (6)

paul_n
Shopify Staff
1817 199 432

Ignore the other response. It's an AI-bot spitting out non-sense.

 

You shouldn't try to load all your products every day - that's a big waste of resources and could cause you to hit API limits.

 

I think you want something like:

Scheduled time trigger

Get product data

For each (product)

...

 

In the "Get product data", you'll need to filter by the metafield value. That metafield needs to have "filterable" turned on in setting to make it usable as a search filter. 

The general syntax for a metafields filter is:

metafields.custom.book_publication_date:"2025-05-21"

 

As documented here:

https://shopify.dev/docs/apps/build/custom-data/metafields/query-by-metafield-value#querying-product...

 

That said, I'm not sure if you can filter by dates just yet. You'll need to try it and see if it works.

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.
benalexanderjon
Tourist
10 0 3

Thanks for the detailed respoinse!

I am going to update the flow so that it adds the tag if:

custom.book_publication_date minus today's date is equal to or less than 30 (ie the book is published in the last 30 days).

 

I have the following set up, but I suspect that the formula I;ve entered in the condition is incorrect:
- {{ "now" | date: "%Y-%m-%d" }}-custom.book_publication_date<=30

 

Any ideas on how to write formulas in conditions?

 

Screenshot 2025-05-21 at 17.52.44.png

paul_n
Shopify Staff
1817 199 432

Conditions do not currently accept liquid in the value field. If you need to do date math in a condition, you'll need to use a run code step before the condition.  One problem with that is that Flow currently does not allow Run code inside For each loops. But I'm not sure the way you are doing this is the right way. What products are you getting and why not use that shop metafield in your query for "Get product data"?

 

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.
benalexanderjon
Tourist
10 0 3

Thanks @paul_n .

 

I think you're right, I will try and Advanced query in the Get product data step.

Do you know how to write a query that checks if the product has a custom.book_publication_date minus today's date is <= 30?

Part of the challenge might be converting custom.book_publication_date from string to a value?
 

 

Thanks!

paul_n
Shopify Staff
1817 199 432

I'm not sure you query by if a date value is < or >. If you could, something like this:

metafields.custom.book_publication_date:>{{ scheduledAt | date_minus:"30 days" }} 

 

You might instead need to query by if it matches the exact date.

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.
benalexanderjon
Tourist
10 0 3

@paul_n - 

 Do you know anyone who could help with writing the query?