What's your biggest current challenge? Have your say in Community Polls along the right column.

Looping through a metafield list of metaobjects that contain single line fields

Looping through a metafield list of metaobjects that contain single line fields

SDGPete
Shopify Partner
2 0 0

Hi,

 

I’m having issues looping through a metaobject which is further defined by a list metafield.

 

I’ve created a metaobject called specifications_table which contains an array of single line fields such as:

  • specifications_table.width
  • specifications_table.length
  • specifications_table.height
  • specifications_table.depth

I’ve then referenced this metaobject as a list metafield definition specifications.table

 

As you may have guessed, it’s being used to generate a product specification table. Some products have variants that require multiple tables (hence the metafield is using list).

 

Data looks like this

 

Table A
    specifications_table.width - 1000mm
    specifications_table.length- 1100mm
    specifications_table.height- 1200mm
    specifications_table.depth- 1300mm
Table B
    specifications_table.width - 1400mm
    specifications_table.length- 1500mm
    specifications_table.height- 1600mm
    specifications_table.depth- 1700mm

The only way I can output the values so far is manually call each named index, but this doesn’t allow for future expansion such as adding weight without manually altering the table.

 

As it is currently:

 

{% if product.metafields.specifications.table != blank %}
    {% for specificationObject in product.metafields.specifications.table.value %}
    <table>
        {% if specificationObject.width!= blank %}
        <tr>
        <td>Width:</td>
        <td>{{ specificationObject.width }}</td>
        </tr>
        {% endif %}
        {% if specificationObject.length!= blank %}
        <tr>
        <td>Length:</td>
        <td>{{ specificationObject.length}}</td>
        </tr>
        {% endif %}
        ....manually repeating/hardcoding the above for all fields
    </table>
    {% endfor %}
{% endif %}

How I’d like it to be:

 

{% if product.metafields.specifications.table != blank %}
    {% for specificationObject in product.metafields.specifications.table.value %}
    <table>
        {% for specificationObjectValue in specificationObject.value %}
        <tr>
        <td>{{specificationObjectValue.index_name}}:</td><-- is it possible to print the index name?
        <td>{{ specificationObjectValue.value}}</td>
        </tr>
        {% endfor %}
    </table>
    {% endfor %}
{% endif %}

 

Is there a way to do the above or is it a lost cause? When I try

 

{% for specificationObjectValue in specificationObject.value %}

It results in nothing

 

When I try:

 

{% for specificationObjectValue in specificationObject %}

I get just one iteration of below (even though there are 3 items:

 

* table: ["gid://shopify/Metaobject/202727326061","gid://shopify/Metaobject/202727457133","gid://shopify/Metaobject/202727522669"]

Natively I’m a PHP developer and this would be achieved with:

 
<?php 
for($specificationObject as $specificationObjectValue){
?>    <table>
    <?php 
    for($specificationObjectValue as $key => $value){
    ?>        <tr>
        <td><?=$key ;?>:</td><-- is it possible to print the index name?
        <td><?=$value;?></td>
        </tr>
        <?php 
        } 
        ?>    </table>
<?php 
} 
?> 

 

Replies 0 (0)