How to ignore empty metafields values in shopify flow action

How to ignore empty metafields values in shopify flow action

jake_mitchell
Shopify Partner
120 2 66

Hi, 

 

I'm trying to pass metafield values from products/lineItems to corresponding MFs on the order. 

 

The code for is pretty janky. I've had to deconstruct and then reconstruct the ["value", "value"] format. 

 

It works, unless there is a product in the order where that metafield is left empty in which case the empty field gets a "," and breaks everything. 

 

[{% for lineItems_item in order.lineItems %}
    {% capture metaDepartment %}
        {% for metafields_item in lineItems_item.product.metafields %}
// THIS LINE NEEDS ADDITION CONDITION {% if metafields_item.namespace == "product" and metafields_item.key == "department" %} {{ metafields_item.value | remove: "[" | remove: "]" }} {% endif %} {% endfor %} {% endcapture%} {{metaDepartment}} {% unless forloop.last %} ,{%endunless%} {% endfor %}]

 I have tried adding and metafield_item.value != empty/blank/nil/null to  it but each time I try none of them work and it still tries to pull through the empty value. 

 

Any idea how that if condition should work or whether there is a way to structure it better so that if an empty value is carried through it doesn't break everything. 

 

thanks 

Replies 2 (2)
jake_mitchell
Shopify Partner
120 2 66

Thanks for giving it a shot. It doesn't work as expected. Or rather, it works how my other attempts have. 

 

It still brings in the empty mf value and tries to include that in the new variable which introduces the extra "," whihc breaks everything. 

 

jake_mitchell_0-1673290089200.png

 

But your code was correct which probably means I've messed it up somewhere else. In itself that's really helpful to know. 

jake_mitchell
Shopify Partner
120 2 66

@Niyeepo  thanks for your help on this. I sorted it. Your solution was good code but it didn't work, so I figured my code was bad. 

 

Changed how the variables are created and structured as MF values and it now works. 

 
{% assign metaValues = ''  %}
    {% for lineItems_item in order.lineItems %}
        {% for metafields_item in lineItems_item.product.metafields %}
            {% if metafields_item.namespace == "product" and metafields_item.key == "department"  %}
                {% assign inner = metafields_item.value %}
                {% assign metaValues = metaValues | append: inner %}
            {% endif %}
        {% endfor %}
    {% endfor %}
{{metaValues | replace: "]", "," | remove: "[" | prepend: "[" | append: "]" | replace: ",]", "]" }}