Using metafields to show different washing instructions

Highlighted
New Member
5 0 0

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

0 Likes
Shopify Expert
9420 9 1259

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
0 Likes
New Member
5 0 0

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?

0 Likes
New Member
5 0 0

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 %}

0 Likes
Shopify Expert
9420 9 1259

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
0 Likes
New Member
5 0 0

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

0 Likes