Solved

Iterate field from an object reference, within a metaobject

addisonwtaylor
Shopify Partner
5 1 1

Hi there,

Long time lurker, first-time poster. I'm looking for some help with metaobjects.


Below I iterate over a list of metaobjects I have set that work perfectly:

{% for support in shop.metaobjects.support.values %}
  <h4>
    <a href="{{ support.website }}">{{ support.organisation }}</a>
  </h4>
  <p>{{ support.categories }}</p>
  <p>{{ support.description | truncate: 200 }}</p>
  <a href="{{ support.website}}">Visit Website</a>
{% endfor %}

However, the issue is the categories, they are a metaobject reference that has a list of entries. Currently, it outputs: 

 

["gid://shopify/Metaobject/99166617930"]

 

Can we get data from these objects and pull through a name/title for instance?

 

TIA!

Accepted Solution (1)

addisonwtaylor
Shopify Partner
5 1 1

This is an accepted solution.

Revisiting this one in case anyone runs into the same issue with metaobject list references. If you want to return the value, you need to map the key name to return the field values. Example below.

 

{% for selectedCategories in shop.metaobjects.support.values %}
{% if selectedCategories %}
{% assign categoryNames = selectedCategories.categories.value | map: 'name' %}
<div>
{% for categoryName in categoryNames %}
<p>{{ categoryName }}</p>
{% endfor %}
</div>
{% endif %}
{% endfor %}

View solution in original post

Replies 5 (5)

ThePixelEdge
Shopify Partner
164 16 17

Hello @addisonwtaylor 
Kindly try with the suggested code, Let us know If you require additional help on this. 

{% for support in shop.metaobjects.support.values %}
  <h4>
    <a href="{{ support.website }}">{{ support.organisation }}</a>
  </h4>
  <p>
    {% for category_id in support.categories %}
      {% assign category = shop.metaobjects.categories[category_id] %}
      {% if category %}
        {{ category.name }}
      {% else %}
        Unknown Category
      {% endif %}
      {% unless forloop.last %}, {% endunless %}
    {% endfor %}
  </p>
  <p>{{ support.description | truncate: 200 }}</p>
  <a href="{{ support.website }}">Visit Website</a>
{% endfor %}



If helpful then please Like and Accept Solution .
Buy me A Coffee

Whatsapp :- For Shopify Design Changes | Shopify Custom Coding | Custom Modifications


Connect with Us in Our DMs | teampixeledge@gmail.com
addisonwtaylor
Shopify Partner
5 1 1

Hey @ThePixelEdge!

Really appreciate your help with this one. Unfortunately, this doesn't seem to work either, it just returns a blank rather than displaying the placeholder too. I've provided some more code snippets below to see if it makes any more sense to you, I'm fairly limited with my knowledge on this one I'm afraid.

 

The main 'support' metaobject returns all the GIDs. Code and output below:

 

{% for selectedCategories in shop.metaobjects.support.values %}
  {% if selectedCategories %}
    {{ selectedCategories.categories }}
  {% endif %}
  {% unless forloop.last %},{% endunless %}
{% endfor %}
["gid://shopify/Metaobject/99266363722"] , ["gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99255255370","gid://shopify/Metaobject/99266363722"] , ["gid://shopify/Metaobject/99266363722"] , ["gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99255255370","gid://shopify/Metaobject/99267117386","gid://shopify/Metaobject/99267084618"] , ["gid://shopify/Metaobject/99255288138","gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99267117386","gid://shopify/Metaobject/99267084618"] , ["gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99266363722"] , ["gid://shopify/Metaobject/99266363722","gid://shopify/Metaobject/99255255370"] , ["gid://shopify/Metaobject/99267117386","gid://shopify/Metaobject/99267084618"] , ["gid://shopify/Metaobject/99255288138","gid://shopify/Metaobject/99255255370","gid://shopify/Metaobject/99266363722"] 

 

 

Then, the reference field returns just the name fields. Code and output below:

 

{% for assignedCategories in shop.metaobjects.supportcategory.values %}
  {% if assignedCategories %}
    {{ assignedCategories | json }}
  {% endif %}
  {% unless forloop.last %},{% endunless %}
{% endfor %}
{"name":"Bereavement"} , {"name":"Hospital Support"} , {"name":"Practical Support"} , {"name":"Grants"} , {"name":"Wellbeing"}

 

addisonwtaylor
Shopify Partner
5 1 1

Bump - if anyone has any ideas on this, it would be much appreciated!

addisonwtaylor
Shopify Partner
5 1 1

Still struggling to find if there's actually a solution for this. Anyone?

addisonwtaylor
Shopify Partner
5 1 1

This is an accepted solution.

Revisiting this one in case anyone runs into the same issue with metaobject list references. If you want to return the value, you need to map the key name to return the field values. Example below.

 

{% for selectedCategories in shop.metaobjects.support.values %}
{% if selectedCategories %}
{% assign categoryNames = selectedCategories.categories.value | map: 'name' %}
<div>
{% for categoryName in categoryNames %}
<p>{{ categoryName }}</p>
{% endfor %}
</div>
{% endif %}
{% endfor %}