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:
‘Product added to store’
‘Get product variant data’
Using this query:
{
product(id: "gid://shopify/Product/") {
id
title
variants(first: 100) {
id
price
sku
}
}
}
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.
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.
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”.
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.
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.
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”
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?
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.
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)