d
Topic
Posts:
5
5 days ago

Using metafields to show different washing instructions

Hello,

we added metafields to our product for care instructions. We have 3 different care instructions for our products. These are specified in the metafield by the numbers 1-3. Now we want to replace the number with a specified text. For instance if the metafield contains the number "2", it should be replaced with "Hand wash only". Something like: if metafield value is "2", display text "Hand wash only".

wash1 = 30 Degree, mild detergents

wash2 = Hand wash only

wash3 = 60 Degree, no bleach

We added the metafield to the code to product-template-liquid. 

We created an if statement like this:

    {% if product.metafield.wash.instructions contains '2' %}
    {% include 'wash2' %}
    {% endif %}

We created a washing-instructions.liquid snippet like this:

{% assign wash1 = '30 Degree, mild detergents' %}

{% assign wash2 = 'Hand wash only' %}

{% assign wash3 = '60 Degree, no bleach' %}

 

Any help is much appreciated, many thanks and all the best from Berlin.

Sam

i
Replies
Jason Shopify Expert freakdesign.com.au
Posts:
9118
Last edited 5 days ago

It looks like your code is almost correct - but it has a tiny error.

It should be this:

{% if product.metafields.wash.instructions contains '2' %}

(metafields not metafields).

Why did you opt for metafields and not tags here?

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
5
5 days ago

Thanks, that one worked. 

{{ product.metafields.wash.instructions }}
    {% if product.metafields.wash.instructions contains '1' %}
    {% include 'wash1' %}
    {% endif %}
    {% if product.metafields.wash.instructions contains '2' %}
    {% include 'wash2' %}
    {% endif %}

How do I get rid of the number "1" which is still displayed on output?

Posts:
5
2 days ago

We tried inserting "remove: '1'", but still ending up with "1" in the text field. Any ideas?

 {{ product.metafields.wash.instructions }}
    {% if product.metafields.wash.instructions == '1' %}
    {% include 'wash1' | remove: '1' %}
    {% endif %}
    {% if product.metafields.wash.instructions contains '2' %}
    {% include 'wash2' %}
    {% endif %}

Jason Shopify Expert freakdesign.com.au
Posts:
9118
2 days ago

You're outputting the metafield value on the first line:

{{ product.metafields.wash.instructions }}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
Posts:
5
2 days ago

Stupid us. Thanks a lot, we just found it ourselves!!