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

Solved

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

WePixel
Shopify Partner
13 0 3

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
}
}
}

Screenshot 2024-05-31 at 21.08.32 Large.jpeg

3) 'For each loop (iterate)'
Setting the list to 'getProductVariantData'

Screenshot 2024-05-31 at 21.08.40 Large.jpeg

4) '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'.

Screenshot 2024-05-31 at 21.47.50 Large.jpeg

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?

Accepted Solution (1)
paul_n
Shopify Staff
1160 143 275

This is an accepted solution.

Yeah, but like I said earlier in the thread, you already have access to the variants on that product via the product.variants field. You don't need a separate action to get the variants. And in fact as you discovered earlier it seems like Default variants (that's what they are called when there is only one) may actually not be included in that query even if you used it. 

 

I would switch that `For each` loop to use product.variants, which should prove that you don't need that Get product variant data action at all. So then delete it and you should be good.

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.

View solution in original post

Replies 22 (22)

paul_n
Shopify Staff
1160 143 275

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.

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.
WePixel
Shopify Partner
13 0 3

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. 

 

paul_n
Shopify Staff
1160 143 275

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

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.
WePixel
Shopify Partner
13 0 3

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)

paul_n
Shopify Staff
1160 143 275

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". 

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.
WePixel
Shopify Partner
13 0 3

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

Screenshot 2024-06-04 at 12.00.28.png

Screenshot 2024-06-04 at 12.00.50.png

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. 

Screenshot 2024-06-04 at 12.06.16.png

Any thoughts?

paul_n
Shopify Staff
1160 143 275

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.

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.
WePixel
Shopify Partner
13 0 3

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. 

paul_n
Shopify Staff
1160 143 275

I'll post back here

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.
WePixel
Shopify Partner
13 0 3

Great thanks!

paul_n
Shopify Staff
1160 143 275

Money option should be there now

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.
WePixel
Shopify Partner
13 0 3

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.

Screenshot 2024-06-05 at 17.53.32.png

 

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"

paul_n
Shopify Staff
1160 143 275

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

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.
WePixel
Shopify Partner
13 0 3

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. 

paul_n
Shopify Staff
1160 143 275

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? 

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.
WePixel
Shopify Partner
13 0 3

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. 

Screenshot 2024-06-06 at 17.21.59.png

 


paul_n
Shopify Staff
1160 143 275

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.

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.
WePixel
Shopify Partner
13 0 3

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:

Screenshot 2024-06-06 at 18.29.52.png

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

Screenshot 2024-06-06 at 18.30.09.png

 

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

 

Compared namespace and key from the metafield definition...

Screenshot 2024-06-06 at 18.27.38.png

...and the 'Update product variant metafield' step.

Screenshot 2024-06-06 at 18.27.13.png

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

 

paul_n
Shopify Staff
1160 143 275

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)

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.
WePixel
Shopify Partner
13 0 3

The reason I have a variant metafield, even though my products don't have any variants, is due to a 3rd party app we need to use to ensure we're compliant in the EU. 

 

When I'm creating a product, I don't have any variants. I want to take the product price and update a variant metafield. (Which should be the default variant for the product).

 

When you don't have any variants, but you do have variant metafields, Shopify puts these fields at the bottom of the page, under the regular product metafields. (As opposed to putting them within the variant view.)

 

If I try to use 'Get product data', as opposed to 'get product variant data', I get this error. 

 

Screenshot 2024-06-07 at 13.01.56.png

Long story short, I have just a single product, but I need to update its variant metadata. 

 

paul_n
Shopify Staff
1160 143 275

This is an accepted solution.

Yeah, but like I said earlier in the thread, you already have access to the variants on that product via the product.variants field. You don't need a separate action to get the variants. And in fact as you discovered earlier it seems like Default variants (that's what they are called when there is only one) may actually not be included in that query even if you used it. 

 

I would switch that `For each` loop to use product.variants, which should prove that you don't need that Get product variant data action at all. So then delete it and you should be good.

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.
WePixel
Shopify Partner
13 0 3

YES!!! It works. 😅

 

Thanks so much for your assistance.