Solved

How can I correctly extract metafields values in JSON code?

AndrewLeb
Tourist
46 0 2

How to update this part of the code to extract metafields values correctly? Or what to paste to the edit query instead?

 

{
"query": {
"objectType": "product",
"id": "product.id",
"metafields": [
{
"namespace": "my_fields",
"key": "style1"
},
{
"namespace": "my_fields",
"key": "year"
},
{
"namespace": "my_fields",
"key": "season1"
},
{
"namespace": "my_fields",
"key": "special"
},
{
"namespace": "my_fields",
"key": "waist"
},
{
"namespace": "my_fields",
"key": "closure"
}
]
}
}

 

 

 

Screenshot 2023-07-20 182115.png

Accepted Solution (1)
paul_n
Shopify Staff
1005 133 240

This is an accepted solution.

 

This gets the metafield by namespace/key and outputs the value. "first" is needed because "where" returns a list.

{%- assign mf_object = product.metafields | where: "namespace", "my_fields" | where: "key", "pattern" | first -%} 
{{ mf_object.value }}

 

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 12 (12)

paul_n
Shopify Staff
1005 133 240

Look at the example queries and the search syntax. You don't need to build the whole query ... just the query string

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.
paul_n
Shopify Staff
1005 133 240

To add... Shopify's product query does not allow searching by metafields yet. https://shopify.dev/docs/api/admin-graphql/2023-07/queries/products#argument-products-query

 

What are you trying to do in your workflow?

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.
AndrewLeb
Tourist
46 0 2

Pass data from metafields to Open AI for text generation.

AndrewLeb
Tourist
46 0 2

Yes, but hot to take a date from Metafields? I don't need all of them.

paul_n
Shopify Staff
1005 133 240

You might not need a query for this. When should the workflow run? Once you have a product in the workflow, you can access all of it's metafields in liquid directly in the OpenAI action

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.
paul_n
Shopify Staff
1005 133 240

See https://community.shopify.com/c/shopify-flow-app/how-to-use-shopify-flow-to-pick-up-color-names-from... for some code

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.
AndrewLeb
Tourist
46 0 2

I know, but how to filter them out? Because they look like this: 

Screenshot 2023-07-20 201738.png

paul_n
Shopify Staff
1005 133 240

I'm not sure where / how you want to filter them out. If you mean access a single value in liquid, my preferred approach is like this:

{%- assign mf_object = product.metafields | where: "namespace", "custom" | where: "key", "color" | first -%} 

 If you mean in a condition, here's example example for order metafields:

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.
AndrewLeb
Tourist
46 0 2

Things are more complex in my case. I am already checking if the required values are in the product: 

Screenshot 2023-07-20 214147.png

 

Then I need to pass exact metafields variables to the Ai prompt here, not just one specific value, to generate a product description based on different parameters Metafield1, Metafield2, Metafield3, and so on:

Screenshot 2023-07-20 214242.png

 

Each metafield can have many variations, not just one:

Screenshot 2023-07-20 214618.png

 

 

 

 

paul_n
Shopify Staff
1005 133 240

I'm not sure what you mean by "variables". I think, b/c of that screenshot, you want all of the possible values for the metafield in that Open AI prompt?  That isn't a metafield value, but I think a metafield definition. I think the choices are stored in validations. You might need to use GraphiQL or a GraphQL client to find the right key name to use for that. Also, you may run into an API limit as query metafields this way is very costly. 

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.
AndrewLeb
Tourist
46 0 2

Variables are metafields: like the one I show on the print screen, how do I add "product.metafields.my_fields.pattern" to the place Metafield1, as I show on the print screen? 

 

What exact piece of code do I have to add to that place?

paul_n
Shopify Staff
1005 133 240

This is an accepted solution.

 

This gets the metafield by namespace/key and outputs the value. "first" is needed because "where" returns a list.

{%- assign mf_object = product.metafields | where: "namespace", "my_fields" | where: "key", "pattern" | first -%} 
{{ mf_object.value }}

 

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.