Solved

How to display product metafield data only when present?

dalemitchley
Shopify Partner
8 0 1

Hi All, 

 

Im hoping I've posted this in the correct place. I'm trying to get metafield data to show the title and data only idf the data is present, at the moment im having to setup multiple templates for our products. 

 

Example.

Im using a Shopify app S-Bulk Excel like product editor to create all my meta fields associated with to the products that all have a unique meta filed value. For instance Height, Length and Width per product.

 

Ive added html code to my template to show the label and then add the relevant data.

 

Im try to get it to only show the label and relevant data only if the is data present, so if there is no data no label will show.  please see example of the html I used below.

 

<p><b><label>Height: </label></b><span productmf='spec/height'></span></p>
<p><b><label>Length: </label></b><span productmf='spec/length'></span></p>
<p><b><label>Width: </label></b><span productmf='spec/width'></span></p>

 

Please let know know if this is possible, much appreciated.

 

Accepted Solution (1)

CodingSolution
Trailblazer
176 12 10

This is an accepted solution.

Use this code

{% for field in product.metafields.spec %}
  {% if field.last %}
    <p>
      <b><label>{{ field.first }}: </label></b>
      <span>{{ field.last }}</span>
    </p>
  {% endif %}
{% endfor %}
banned

View solution in original post

Replies 2 (2)

CodingSolution
Trailblazer
176 12 10

This is an accepted solution.

Use this code

{% for field in product.metafields.spec %}
  {% if field.last %}
    <p>
      <b><label>{{ field.first }}: </label></b>
      <span>{{ field.last }}</span>
    </p>
  {% endif %}
{% endfor %}
banned
dalemitchley
Shopify Partner
8 0 1

Thanks for this, it works great. Appreciate the help.