Re: Liquid Question: Sorting Metafields By Unit

Liquid Question: Sorting Metafields By Unit

lamez
Visitor
3 0 0

Hello,

I did ask this in a different topic, but I think it is more applicable in this topic.

 

I am new to Liquid, however, I have been searching the docs, and I am still having some trouble.

 

What would the syntax look like for sorting metafields by their unit of measurement? I have the metafields I want to display in the namespace specs..

 

Here is the current liquid I have written:

{% if product_specs.size > 0 %}
  <div id="specifications">
    <ul>
      {% for spec in product.metafields.specs -%}
        <li><strong>{{ spec.first | capitalize }}</strong>: {{ spec.last.value }}</li>
      {%- endfor %}
    </ul>
  </div>
{% endif %}

 

Here is the output:

Height: 55 mm
Weight: 2 oz
Width: 25 mm

 

So, I want Height and Width displayed next to each other, any thoughts or help on this would be greatly appreciated! 

Replies 2 (2)

MetafieldsGuru
Shopify Partner
160 32 102

Hi @lamez ,

 

Welcome to the Shopify community 🙂

While Liquid does have some tricks for sorting arrays, I'm afraid there's no default solution to your specific problem. However, you can do that by applying conditional logic to your code as shown here. Or you can add numeric indexes to metafield keys and let Liquid do the job for you. Once the array is sorted, you can simply strip those indexes from the keys prior to displaying them on the product page.

Check out Metafields Guru, the #1 ranked metafields app.

Bulk editor | Data import/export | Metafield sets | Browser extension
lamez
Visitor
3 0 0

@MetafieldsGuru thank you for your response, giving the examples of the syntax, and the idea of adding in an index is extremely helpful!

 

Thank you so much!