Discuss and resolve questions on Liquid, JavaScript, themes, sales channels, and site speed enhancements.
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!
Solved! Go to the solution
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 %}
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 %}
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"}
Bump - if anyone has any ideas on this, it would be much appreciated!
Still struggling to find if there's actually a solution for this. Anyone?
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 %}
Dropshipping, a high-growth, $226 billion-dollar industry, remains a highly dynamic bus...
By JasonH Nov 27, 2024Hey Community! It’s time to share some appreciation and celebrate what we have accomplis...
By JasonH Nov 14, 2024In today’s interview, we sat down with @BSS-Commerce to discuss practical strategies f...
By JasonH Nov 13, 2024