Re: splitting {{ metafield }} output loop

Solved

How to split namespace and key in a product metafield output loop?

SpotterJ
Shopify Partner
67 1 17
{% for metafield in product.metafields.namespace %}
	<li>{{ metafield }} </li>
{% endfor %}

outputs

<li>namespacekey</li>

which is great, but how do I split namespace and key?

 

The direct access is great and all but it gets pretty cumbersome:

{% if product.metafields.knife.blade_engraving.value != 'blank' %}
	<li>
		Blade engraving: {{ product.metafields.knife.blade_engraving.value | metafield_tag }}
	</li>
{% endif %}

repeat for all metafields (in my case 20).

 

It'd be more elegant if we could loop through this.

Is there a way to grab the Name and Description from the Shopify backend?

Learning something new every day
Accepted Solution (1)
MinderSolutions
Shopify Partner
6 1 8

This is an accepted solution.

Just checked it and your description is true... It's counterintuitive.  The mechanism they provide is .first and .last properties I works on the json metafield type and others as well:
ex>

 
{% liquid
  assign ns = shop.metafields.zzz
  for k_v in ns
    assign k = k_v.first
    assign v = k_v.last

    echo '-------'
    echo v.value.a
    echo '------- <br>'
  endfor 
%}
 noting the unconventional v.value.yourprop syntax and the .last prop to get the to value in the first place.   That is two extra steps  but it does work and the looping works as well
so: json type:
Screen Shot 2022-10-02 at 7.55.20 PM.png
yields the values: 
 
-------april-------
-------bat-------
-------cat-------

View solution in original post

Replies 6 (6)

LitExtension
Shopify Partner
4860 1002 1159

Hi @SpotterJ,

You can't loop over a namespace to get each individual metafield. Refer https://shopify.dev/api/liquid/objects/metafield#direct-access

So you can't "for" it, you can just get out each metafield.

Hope it helps!

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify
SpotterJ
Shopify Partner
67 1 17

That's not strictly true. With my example above I do get a list of all the metafields namespaces and their values, however there's no space between the two. 

Learning something new every day
LitExtension
Shopify Partner
4860 1002 1159

Hi @SpotterJ,

Yes, I totally know this, you will be returned a text including the name and value.

But currently, there is no way to separate it. You can refer to shopify's instructions.

If it helped you solve your issue, please mark it as a solution. Thank you and good luck.

LitExtension - Shopping Cart Migration Expert
Check out our Shopify migration app to migrate your online store to Shopify
SpotterJ
Shopify Partner
67 1 17

I ended up with direct access, couldn't find a way to split the two. 

Learning something new every day
MinderSolutions
Shopify Partner
6 1 8

This is an accepted solution.

Just checked it and your description is true... It's counterintuitive.  The mechanism they provide is .first and .last properties I works on the json metafield type and others as well:
ex>

 
{% liquid
  assign ns = shop.metafields.zzz
  for k_v in ns
    assign k = k_v.first
    assign v = k_v.last

    echo '-------'
    echo v.value.a
    echo '------- <br>'
  endfor 
%}
 noting the unconventional v.value.yourprop syntax and the .last prop to get the to value in the first place.   That is two extra steps  but it does work and the looping works as well
so: json type:
Screen Shot 2022-10-02 at 7.55.20 PM.png
yields the values: 
 
-------april-------
-------bat-------
-------cat-------
mukulAGT
Shopify Partner
1 0 0

Hi @MinderSolutions 

You saved my day dude it is awsome Thanks For help man.

KUDOS 👍😊