0

I am using metaobjects and refernce them in certain products. In my cart liquid, I want to loop over the products and find all references to the metaobject. If one is linked to a product in the cart, I need to access additional data in the metaobject.

How can I access the metaobject data in liquid? Or how do I use the generated GIDs?

I am currently finding the metaobject like this: {% for item in cart.items %} {% if item.product.metafields.custom.object_upsell %} {{ item.product.metafields.custom.object_upsell }} --> here, I get e.g. ["gid://shopify/Metaobject/9076982","gid://shopify/Metaobject/9044214"] {% endif %} {% endfor %}

When I try and loop over the object like this, I get no output at all: {% assign referencedObjects = item.product.metafields.custom.upsell_to %} {% for object in referencedObjects %} {{ object }} {% endfor %}

I expected to still get an output of the gids, here.

anna
  • 1
  • 2
  • I think you just need .values at the end of the reference to the metaobject {% for item in cart.items %} {% if item.product.metafields.custom.object_upsell %} {% assign referencedObjects = item.product.metafields.custom.object_upsell.values %} {% for object in referencedObjects %} {{ object }} {% endfor %} {% endif %} {% endfor %} – pete Mar 08 '23 at 16:49
  • Thank you @pete! Unfortunately, that does not seem to have any output. `{{item.product.metafields.custom.object_upsell}}` --> Outputs an array as mentioned. `{{item.product.metafields.custom.object_upsell.values}}` --> Outputs nothing – anna Mar 09 '23 at 07:37
  • I still think this is close check out this post https://community.shopify.com/c/technical-q-a/how-do-i-loop-through-a-text-metafield-list/td-p/1615541 try .value not .values this should target the array inside the metafield ``` {% for item in cart.items %} {% if item.product.metafields.custom.object_upsell %} {% assign referencedObjects = item.product.metafields.custom.object_upsell.value %} {% for object in referencedObjects %} {{ object }} {% endfor %} {% endif %} {% endfor %} – pete Mar 09 '23 at 10:35

1 Answers1

0

Add "| first" after value try to fetch

{% assign list =  product.metafields.custom.product_varients.value | first   %}

{% for pro in  list.p_list.value  %}
  {% assign producturl = "/products/" | append:pro.handle %}
  {% assign index = forloop.index | minus: 1 %}
  <a href="{{ producturl }}">
    <span class="dot {% if producturl == product.url %}dot-selected{% endif %}" style="background-color: {{ list.p_color.value [index] }};"></span>
  </a>
{% endfor %}
Tyler2P
  • 2,324
  • 26
  • 22
  • 31