Way to use same metafield in multiple conditions?

My store sells Tool Fees. If you pay the fee, you get to use the tool for a year. (We’re a makerspace. Please don’t judge)

Each customer has a metafield that stores their Tool Fee expiration date. In this case it’s called tool_fee_wood_shop

Therefore, I’m trying to make a Shopify Flow with the following logic.

  1. When Wood Shop Tool Fee is paid
  2. if tool_fee_wood_shop does not exist, assign tool_fee_wood_shop = today’s date 1 year in future
  3. else, if tool_fee_wood_shop < ‘now’, assign tool_fee_wood_shop = today’s date 1 year in future
  4. else tool_fee_wood_shop >= ‘now’, assign tool_fee_wood_shop = that date 1 year in future

I can create steps 1 and 2 in a Shopify Flow.

  1. Trigger: order paid
  2. Condition: if tool_fee_wood_shop is empty

But when I try to create step 3, Condition: if tool_fee_wood_shop < ‘now’, the system won’t let me selecttool_fee_wood_shopa second time. It says “Alias Added,” but I can’t see how to put that metafield into the comparison.

How can I use the same metafield in two conditions?

You can’t compare 2 variables in Flow condition. (update – now you can sometimes)

But, you can do that in update metafield action value.

However, you can’t get current date/time in Flow.

If your trigger is “Order is paid” then you can rather use {{order.createdAt}} or similar variable.

Say like this:

{%- liquid
  assign orderDate = order.createdAt |  date: "%Y%m%d" %}
  assgn  mfValue = order.customer.metafield.tool_fee_wood_shop | default: order.createdAt | date: "%Y%m%d"

  if mfValue <= orderDate
    assign newValue = order.createdAt | date_plus: "1 year"
  else
    assign newValue = order.customer.metafield.tool_fee_wood_shop | date_plus: "1 year"
  endif
-%}{{ newValue }}
1 Like

I’m afraid I can’t get the code to work. Here’s the code in the “Update customer metafield” step:

{%- liquid
  assign orderDate = order.createdAt |  date: "%s"
  assign  mfValue = order.customer.metafields.value | default: order.createdAt | date: "%s"

  if mfValue <= orderDate
    assign newValue = order.createdAt | date_plus: "1 year"
  else
    assign newValue = order.customer.metafields.value | date_plus: "1 year"
  endif
-%}
{{ newValue }}

Every time, no matter what, the tool_fee_wood_shop is set to today’s date plus one year. It’s almost like the if is completely ignored?

Unfortunately, that’s not all. For whatever reason, the code to advance the date stored in the metafield by 1 year doesn’t work either. When I put just that line in:

{%- liquid
assign newValue = order.customer.metafields.value | date_plus: "1 year"
-%}
{{ newValue }}

…the date doesn’t change at all.

Am I trying to do something that Flow simply can’t do?

Quick drive by,
‘now’ doesn’t mean anything it needs a date filter into a variable.
https://help.shopify.com/en/manual/shopify-flow/concepts/variables#filters

Everything you’ve describe should be possible but you or someone else has to put the work in.

My bad, I could’ve worded that better. The text you referenced was just to explain what I wanted the code to do. It wasn’t supposed to be actual liquid code. I appreciate the response, though!

I really hope it’s possible!

{% assign now_in_seconds = now | date: "%s" %}
etc, see the flow docs, or search any existing posts needing date conditions etc.

Of course – the order.customer.metafields.value is not the proper way to address a MF in Flow.
Therefore mfValue always uses default and turns to be the same as orderDate

When you reference a MF in Flow it creates an alias and you must use that one.
I guess, if your metafield is
customer.metafields.custom.tool_fee_wood_shop, then the alias would be toolFeeWoodShop and proper reference would be customer.toolFeeWoodShop.value

You can use “Add variable” functionality to see it and then use in my code (which was also not 100% proper – sorry about that :slight_smile: )

I’d be thinking something like this should be the next step I’d try:

{%- liquid
  assign orderDate = order.createdAt |  date: "%Y%m%d" %}
  assgn  mfValue = order.customer.toolFeeWoodShop.value | default: order.createdAt | date: "%Y%m%d"

  if mfValue <= orderDate
    assign newValue = order.createdAt | date_plus: "1 year"
  else
    assign newValue = order.customer.toolFeeWoodShop.value | date_plus: "1 year"
  endif
-%}{{ newValue }}

And the same with your second code:

{%- liquid
assign newValue = order.customer.metafields.value | date_plus: "1 year"
-%}
{{ newValue }}

This flow would likely produce an error like: Liquid error: invalid date provided to date_plus
You should use metafield alias as well:

{%- liquid
assign newValue = order.customer.toolFeeWoodShop.value | date_plus: "1 year"
-%}
{{ newValue }}

Also, Flow has pretty good job logging which you can extend by using a “Log” action to better understand where the problem is.


if my post is helpful, please like it ♡ and mark as a solution -- this will help others find it
1 Like

Sorry to keep asking questions. First of all, thank you, tim_1, your code worked great!

How do you find the alias that Flow assigns to a metafield? In your comment above you guessed the alias name and were spot on.

Now I’m trying to create a Metal Shop Tool Fee flow. It works just like the Wood Shop Tool Fee flow except it’s looking at a different metafield. I took a similar guess at the alias for that metafield but my guess was incorrect.

I tried to find the answer myself. This page offered the following under Get metafield data

    1. Click Add a variable in an action, or Add criteria in a condition.
    2. Go to the resource where the metafield is stored (such as product, customer, order).
    3. Choose metafield from the list of fields. (note: don’t choose metafields as this provides a list of all metafields).
    4. In the popup, select the metafield using the namespace and key, make a note of the alias name provided, and click Add.

But when I’m choosing the metafield I want the action to update it doesn’t show me the alias for it.

You do not need to guess – click “add variable”, select your object from the list, scroll to metafield (not metafields!) and pick the alias from there.
Basically – the next step from your last screenshot – after you make selection, it will create an alias and then you click this alias to add to the input field.


1 Like