skip to Main Content

I’m trying to fetch the assigned products to a product in the metafields. The content type is "Product (List)" (not sure if this is the correct type but it seems so)

I tried to render them through this liquid code:

<section class="product-list">
  <div>
    {% for bundle_product in product.metafields.custom.bundle %}
    {% include 'single-product-card', bundle_product %}
    {% endfor %}
  </div>
</section>

And here’s the definition:
enter image description here

However, nothing is being displayed. I logged the output of product.metafields.custom.bundle and it returned this:

['gid://shopify/Product/7580521365703', 'gid://shopify/Product/7510314975431']

How should I properly loop on them?

2

Answers


  1. {% if article.metafields.custom.mentioned_products %}
    {% assign mentionproduct = article.metafields.custom.mentioned_products.value %}

    <div class="related-products" id="related-products-{{ block.id }}">
      {% for prod in mentionproduct %}
        {% if prod.available == true %}
          <a href="{{ prod.url }}" class="related-product" aria-label="{{prod.title}}" title="{{ prod.title }}">
            <img class="related-product__image" src="{{ prod.images[0] | img_url: '500x' }}" alt="{{ prod.title }}">
          </a>
        {% endif %}
      {% endfor %}
    </div>
    

    {% endif %}

    Login or Signup to reply.
  2. The answer above looks correct but I thought I would add a bit of explanation. The thing to do is to assign the metafield to a variable but to do that you need to use the .value property of the metafiled.

    So try changing your original code this:

    <section class="product-list">
      <div>
       {% for bundle_product in product.metafields.custom.bundle %}
       {% include 'single-product-card', bundle_product %}
       {% endfor %}
      </div>
    </section>
    

    To this:

    <section class="product-list">
      <div>
       {% assign bundle_products = product.metafields.custom.bundle.value %}
       {% for bundle_product in bundle_products %}
       {% include 'single-product-card', bundle_product %}
       {% endfor %}
      </div>
    </section>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search