Populate a variant metafield with the price of a product at the point the product is created

Hi guys,

I’m really struggling to figure out how to create a flow that will update a variant metafield with a product’s price at the point a product is created.

My workflow so far has been:

  1. ‘Product added to store’

  2. ‘Get product variant data’
    Using this query:

{
product(id: "gid://shopify/Product/") {
id
title
variants(first: 100) {
id
price
sku
}
}
}

  1. ‘For each loop (iterate)’
    Setting the list to ‘getProductVariantData’

  1. ‘Update product variant metafield’
    Setting the Namespace and Key.

The value to:

{% for getProductVariantData_item in getProductVariantData %}
  {{getProductVariantData_item.price}}
{% endfor %}§

and the Type as ‘Decimal’.

However, this doesn’t work. In order to debug, I used the internal email feature and can see the workflow is getting no results out of the For Each loop.

Any idea where I’m going wrong?

Your query filter is way off. It’s not GraphQL that you enter there but a Shopify search syntax like tag:foo. If you look at the non-advanced examples you will see some ideas. Or look here: https://help.shopify.com/en/manual/shopify-flow/reference/actions/get-product-variant-data

So that means you are getting 0 results in your workflow. I’d recommend using “log output” instead of updating the metafield at first, as it allows you to see the output without making the wrong change.

Thanks for your reply. Though I’m still a little lost still.

I’ve updated the Query in the ‘Get product variant data’ to id:{{product.id}}

Then just attempted to ‘Log output’ to see if even that’s correct, using:

{% for getProductVariantData_item in getProductVariantData %}
{{getProductVariantData_item.price}}
{% endfor %}

But that doesn’t work either. I’m clearly approaching this in the wrong way.

Any guidance would be a huge help.

The ID you pass to query filters is usually just the numeric part, so it would be {{ product.legacyResourceId }}

Ok thanks. One step closer. Though the log output doesn’t show me the price:
[“Product ID: gid://shopify/Product/9444634788124\nProduct Title: Omnibus 102\nVariants:\n”]

My product doesn’t have any actual variants. But I’d of assumed it would still how me a price as it technically has one variant. (The default product price)

I don’t think you are getting any variants back via that API call. That said, if all you are looking to do is get the variants for the current product, you don’t need the query at all. Just click “add a variable” and choose “product / variants / price”.

Ok thanks. I’ve removed the query and seem to be a little closer.

The issue I’m seeing now is that the ‘Update product variant metafield’ step requires you to set the ‘type’.

The variant metafield I’m trying to update is a ‘Money’ field. There isn’t a money option in the ‘Update product variant metafield’ type dropdown. I’ve been using ‘Decimal’ as it seems to be the most suitable.

Any thoughts?

Hi, looks like we are indeed missing the money field for whatever reason (we need to update them as new types arrive). I’ll see if we can update the action for this type…might take a few days though.

Great! Will you be able to let me know when it’s added to the available types list? Or will I have to keep checking the dropdown?

Many thanks.

I’ll post back here

Great thanks!

Money option should be there now

Great. Thanks for the quick turnaround!

I’ve tested this out, using the same steps as above, but using the new Money type. But now I’m running into the following.

Thought I could maybe filter the output like this:
{% for variants_item in product.variants %}{{ variants_item.price | money_with_currency }}{% endfor %}

or

{% for variants_item in product.variants %}{{ variants_item.price | money }}{% endfor %}

But both result in “Liquid error: undefined filter money”

These filters are not available in the liquid library that Flow uses. See https://shopify.github.io/liquid/

Would this make more sense?

{% for variants_item in product.variants %}

{

“amount”: {{ variants_item.price }},

“currency_code”: “EUR”

}

{% endfor %}

This doesn’t result in any errors, but the variant metafield doesn’t get updated.

Feel like I’m chasing my own tail a bit with this one.

No, because that money field only accepts a single JSON object, not a list of them. You could add a condition like {% if forloop.first %} to just output json on the first, or some other condition to check the variant that you care about.

Edit: actually I’m not sure why you are checking product.variants here and not just the single variant. What does your workflow look like now?

Thanks Paul. Gave this a go:

{% for variants_item in product.variants %}
{% if forloop.first %}
{
“amount”: {{ variants_item.price }},
“currency_code”: “EUR”
}
{% endif %}
{% endfor %}

… but nothing seems to happen. No errors, but nothing updates.

Here’s the current flow. I’m not sure where to go from here. Any advice would be greatly appreciated.

Put it in log output to see what it outputs. If it’s what you expect, then the action is the problem…usually that means you’ve entered the wrong namespace or key or both.

Using:

{% for variants_item in product.variants %}{% if forloop.first %}{
“amount”: “{{ variants_item.price }}”,
“currency_code”: “EUR”
}{% endif %}{% endfor %}

I tired logging that output and I get this:

Which looks like it’s the same format as the documentation:

Then went back to ‘Update product variant metafield’, but no luck.

Compared namespace and key from the metafield definition…

…and the ‘Update product variant metafield’ step.

All looks fine. Not sure what I could be missing.

That all looks right. Why are you still using “Get variant data”? You could be updating a different variant that one on the product. And are you sure that the variable isn’t actually set? It likely is, but you are looking at the wrong variant (not necessarily the one that comes first in the UI)