All things Shopify and commerce
I would like to render all the meta fields from a product in an accordion as a table with metafield's name on left column and metafield's value on right column.
I already managed to render values doing something like this in the accordion.liquid file :
{%- assign features_items = product.metafields.custom -%}
render 'accordion-item', title: title, content: features_items, add_content_class: add_content_class, toggle_icon: toggle_icon, item_open: item_open
Then :
<ul class="cc-accordion-features">
{% for feature in content %}
{%- liquid
assign label = feature[0]
assign value = feature[1]
-%}
<li>
<span class="label">{{ label }}</span>
<span class="value">{{ value }}</span>
</li>
{% endfor %}
</ul>
This works pretty fine, but the value is the namespace of the meta fields, so it gives me something like "My_meta_field". How would it be possible to render the names of the meta field ?
Solved! Go to the solution
This is an accepted solution.
Hello, this code snippet is responsible for outputting the names and values of meta fields. I hope this helps
{%- assign values_and_meta = blank -%}
{%- assign title_list = blank -%}
{%- assign value_list = blank -%}
{%- for metafield in product.metafields.custom-%}
{%- for item in metafield -%}
{%- assign index = forloop.index0 | modulo: 2 -%}
{%- if index == 0 -%}
{%- assign title_list = title_list | append: item | append: '%%%' -%}
{%- else -%}
{%- assign value_list = value_list | append: item | append: '%%%' -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- assign splitted_titles = title_list | split: "%%%" -%}
{%- assign splitted_values = value_list | split: "%%%" -%}
{%- for value in splitted_values -%}
title: {{- splitted_titles[forloop.index0] }}
value: {{- value }}<br>
{%- endfor -%}
This is an accepted solution.
Hello, this code snippet is responsible for outputting the names and values of meta fields. I hope this helps
{%- assign values_and_meta = blank -%}
{%- assign title_list = blank -%}
{%- assign value_list = blank -%}
{%- for metafield in product.metafields.custom-%}
{%- for item in metafield -%}
{%- assign index = forloop.index0 | modulo: 2 -%}
{%- if index == 0 -%}
{%- assign title_list = title_list | append: item | append: '%%%' -%}
{%- else -%}
{%- assign value_list = value_list | append: item | append: '%%%' -%}
{%- endif -%}
{%- endfor -%}
{%- endfor -%}
{%- assign splitted_titles = title_list | split: "%%%" -%}
{%- assign splitted_values = value_list | split: "%%%" -%}
{%- for value in splitted_values -%}
title: {{- splitted_titles[forloop.index0] }}
value: {{- value }}<br>
{%- endfor -%}
Thanks for your answer, interesting code snippet !
I managed to display name with replacing underscores form namespaces by spaces with a liquid replace filter :
<ul class="cc-accordion-features">
{% for feature in content %}
{%- liquid
assign label = feature[0]
assign value = feature[1]
assign formatted_label = label | replace: '_', ' '
-%}
<li>
<span class="label">{{ formatted_label }}</span>
<span class="value">{{ value }}</span>
</li>
{% endfor %}
</ul>
User | RANK |
---|---|
40 | |
37 | |
24 | |
22 | |
15 |
Transform this holiday season into a shopping spree. Plus, learn how to effortlessly open ...
By Jasonh Dec 8, 2023Make the shift from discounts to donations, and witness your business not only thrive fina...
By Holly Dec 4, 2023On our Shopify Expert Marketplace, you can find many trusted third party developers and fr...
By Arno Nov 27, 2023