Limiting returned data by metafield value in advanced flow workflow

jake_mitchell
Shopify Partner
120 2 53

Hi, 

 

I am using the advanced flow action to get order data from the last day and then send an internal email. 

 

I won't go over the full use case but I'd like to get product titles only when that product has a certain metafield value

 

This is what I have (where the CAPS things are replaced with the real values)

 

  {% for getOrderData_item in getOrderData %}
    {% for lineItems_item in getOrderData_item.lineItems %}
        {% if lineItems_item.product.metafields.namespace == "NAMESPACE" and lineItems_item.product.metafields.key == "KEY" and lineItems_item.product.metafields.value == VALUE%}
            {{lineItems_item.product.title}}
        {% endif %}
  {% endfor %}
  {% endfor %}
 
This returns nothing at all despite the metafield value definitely being present. 
 
Does anyone have any pointers here? 

Thanks, 
Replies 8 (8)

lilyliward
Shopify Staff
18 1 7

Hi, 

 

Each product could have multiple metafields. I believe that you need to do a for loop on the metafields as well. Something like:

....

{% for metafield_item in lineItems_item.product.metafields %}

 {% if  if metafields_item.key == "key" and metafields_item.value="value" %}

   {{lineItems_item.product.title}}

....

 

Lily

To learn more visit the Shopify Help Center or the Community Blog.

jake_mitchell
Shopify Partner
120 2 53

Thanks! 

I tried something like that and it still returns nothing

  {% for getOrderData_item in getOrderData %}
    {% for lineItems_item in getOrderData_item.lineItems %}
        {% for metafields_item in lineItems_item.product.metafields %}
            {% if metafields_item.key == "KEY" and metafields_item.value == "VALUE" %}
               {{lineItems_item.product.title}}
            {% endif %}
        {% endfor %}
    {% endfor %}
{% endfor %}

 

Wondering if there is a weird scope thing going on that I don't understand because the metafield value is definitely present on at least one item. 

paul_n
Shopify Staff
1024 133 243

I might start be removing the IF statement and echoing out the metafield values like {{ metafields_item.value }}. If it has metafields and they don't print out then, your scoping idea may be correct. 

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.
jake_mitchell
Shopify Partner
120 2 53

Thanks @paul_n 

I've removed the if statements and used this instead

 

 

{% for getOrderData_item in getOrderData %}
    {% for lineItems_item in getOrderData_item.lineItems %}
        {% for metafields_item in lineItems_item.product.metafields %}
            {{lineItems_item.product.title}} {{metafields_item.value}}
        {% endfor %}
    {% endfor %}
{% endfor %}

 

 

That gives me a list of the products with their various metafield values next to it

 

Product 1 Metafield_value

Product 1 Metafield_value

Product 1 Metafield_value

Product 2 Metafield_value

 

etc. 

 

This seems to imply that I can access those metafields and product.title in those loops so the question remains why the if statement stops it working 

Really odd. It's not a particularly complex bit of liquid and I think the syntax is good and the mf namespace.keys.values all labelled correctly. 

So really not sure why I can't limit the data like this. 

p.s. I have also tried putting a condition in front of this to look for the metafield namespace.key.value but that also doesn't work. 

paul_n
Shopify Staff
1024 133 243

Are you sure you want to check the value in the "if" and not the "namespace" and "key"?  Are you sure the key name is what you think it is? FYI, Flow does not use dot notation with metafields yet so things like "metafields.custom.my_key" will not work.

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.
jake_mitchell
Shopify Partner
120 2 53

Yep. I'm sure i want to check the value. 

 

That metafield is a true/false one I want to only output the product titles where it was set as true. 

 

The actual use case is that we have some items that as a charity shop we have some items that are gift aidable. I would like to be able to output product details in from the advanced workflow where the gift aid metafield is set to true. 

 

 

It's odd that you say flow doesn't work with dot notation because I'm using it in a few other places fine. unless I'm misunderstanding what you're saying of course. 

paul_n
Shopify Staff
1024 133 243

By dot notation, I mean you cannot access metafields specifically like {{ order.metafields.my_name.my_key }} as you can in storefront liquid. Otherwise it works.

 

I think your condition has the wrong value or key name. I think to check for a boolean metafield it may be case sensitive so has to be the string "True" 

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.
jake_mitchell
Shopify Partner
120 2 53

Hi @paul_n 

 

Thanks for the help. I'm afraid it still doesn't work. 

Here's the metafield

jake_mitchell_0-1675071395549.png

 

And here's the code in the flow action

 

jake_mitchell_1-1675071472919.png

 

It still returns nothing. 

 

At this point I'm thinking that the advanced workflows maybe just won't be able to do what we thought they could. The only thing I can think of would be whether we could put the metafield value filter on getOrderData action itself. 

 

jake_mitchell_2-1675071607010.png

 

Is that possible do you know?