Find a referenced metaobject for each item in cart

2k Views Asked by At

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.

1

There are 1 best solutions below

0
On

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 %}