Solved

Meta objects in product metafields + related metafield

studioboskant
Shopify Partner
4 1 1

Hi everyone, 

 

I'd like to use different meta object entries for several product metafields.
For instance : 

Key feature 1 (metafield) = Meta object entry 1

Key feature 2 (metafield) = Meta object entry 2

...

For another product it could be for example : 

Key feature 1 = meta object entry 4

Key feature 2 = meta object entry 8 

 

I've been able to do this: 

 

{% assign keys = shop.metaobjects.key_feature_content.values %}
{% for key in keys %}
<div>
{{ key.key_feature_title.value }}
{{ key.key_feature_description.value }}
</div>
{% endfor %}

 

 

but this content is also related to an image. The image is different for each product (so it's a metafield with file) while some key feature titles and descriptions will be the same. 
From this code, I don't how I could insert the related image (img_keyfeature_1,  img_keyfeature_2, img_keyfeature_3 .... and so on until 6).

 

Any ideas?

Thank you for your help! 

Accepted Solution (1)

studioboskant
Shopify Partner
4 1 1

This is an accepted solution.

Well if anyone faces the same situation, I finally managed to do this :

 

{% assign keys = shop.metaobjects.key_feature_content.values %}
    {% for key in keys %}
      {% assign this_loop = forloop.index | modulo: 7 -%}
      <div class="feature">
        <div class="image_key">
          {% if this_loop == 1 %}
            <img src="{{ product.metafields.custom.img_keyfeature_1 | file_url }}">
          {% elsif this_loop == 2 %}
            <img src="{{ product.metafields.custom.img_keyfeature_2 | file_url }}">
          {% elsif this_loop == 3 %}
            <img src="{{ product.metafields.custom.img_keyfeature_3 | file_url }}">
          {% elsif this_loop == 4 %}
            <img src="{{ product.metafields.custom.img_keyfeature_4 | file_url }}">
          {% elsif this_loop == 5 %}
            <img src="{{ product.metafields.custom.img_keyfeature_5 | file_url }}">
          {% elsif this_loop == 6 %}
            <img src="{{ product.metafields.custom.img_keyfeature_6 | file_url }}">
          {% endif %}
        </div>
        <div class="desc_key">
          <h4>{{ key.key_feature_title.value }}</h4>
          <p>{{ key.key_feature_description.value }}</p>
        </div>
      </div>
    {% endfor %}

 

 

Works perfectly for me 🙂

Note: If you select the same entry for several metafields, only one of them will show. You must have 6 different entries.

View solution in original post

Reply 1 (1)

studioboskant
Shopify Partner
4 1 1

This is an accepted solution.

Well if anyone faces the same situation, I finally managed to do this :

 

{% assign keys = shop.metaobjects.key_feature_content.values %}
    {% for key in keys %}
      {% assign this_loop = forloop.index | modulo: 7 -%}
      <div class="feature">
        <div class="image_key">
          {% if this_loop == 1 %}
            <img src="{{ product.metafields.custom.img_keyfeature_1 | file_url }}">
          {% elsif this_loop == 2 %}
            <img src="{{ product.metafields.custom.img_keyfeature_2 | file_url }}">
          {% elsif this_loop == 3 %}
            <img src="{{ product.metafields.custom.img_keyfeature_3 | file_url }}">
          {% elsif this_loop == 4 %}
            <img src="{{ product.metafields.custom.img_keyfeature_4 | file_url }}">
          {% elsif this_loop == 5 %}
            <img src="{{ product.metafields.custom.img_keyfeature_5 | file_url }}">
          {% elsif this_loop == 6 %}
            <img src="{{ product.metafields.custom.img_keyfeature_6 | file_url }}">
          {% endif %}
        </div>
        <div class="desc_key">
          <h4>{{ key.key_feature_title.value }}</h4>
          <p>{{ key.key_feature_description.value }}</p>
        </div>
      </div>
    {% endfor %}

 

 

Works perfectly for me 🙂

Note: If you select the same entry for several metafields, only one of them will show. You must have 6 different entries.