Using Metafield as condition for product grid formatting

Solved

Using Metafield as condition for product grid formatting

jknupp
Tourist
5 1 1

Hi all,

 

I'm trying to use a product metafield as a "condition" to do some formatting on collection pages.  Here's my set up:

 

Each product has a metafield of "product type".  The field is:  metafields.custom.prod_format.  This is set up as a drop down list that allows me to pick the specific type of project.  Only one selection is allowed.

 

On collections pages, I want some additional metafields to be displayed if the prod_format = "Merchandise".  For everything else, a slightly different set of data and formatting.

 

Here's my code from the card-product.liquid file

 

{% if card_product.metafields.custom.prod_format == "Merchandise" %}
         <p> Product Series:</br>
         {{ card_product.metafields.custom.series }} </p>
{% else %}
         <p><b> {{ card_product.metafields.custom.series | metafield_text }} Book #

         {{ card_product.metafields.custom.number_in_series }}</b></p>
         <p><i> {{ card_product.metafields.custom.tagline }} </i></p>
{% endif %}

 

From what I can tell my issue is in how the card_product.metafields.custom.prod_format is returning because it never hits a situation where the first "if" condition is true.  So, despite products having that field set correctly, it never formats those products correctly in the grid.  It always formats them in the "else" condition.

 

When I return card_product.metafields.custom.prod_format in text I get [Merchandise] and not Merchandise, unless I use the | metafield_text tag at the end.

 

So I'm guessing that the If/else process is seeing [Merchandise] as the value and not Merchandise.

 

How do I write this so it evaluates correctly?

 

I've tried:

  • {% if card_product.metafields.custom.prod_format == "Merchandise" %}
  • {% if card_product.metafields.custom.prod_format == [Merchandise] %}
  • {% if card_product.metafields.custom.prod_format == "[Merchandise]" %}
  • {% if card_product.metafields.custom.prod_format == '["Merchandise" ]' %}

Nothing works so that the first condition is true.

 

Any thoughts?  

 

Thanks in advance.

 

-Justin

Accepted Solution (1)

jknupp
Tourist
5 1 1

This is an accepted solution.

I just solved this.

 

The [value] lead me to think that it was returning an array value.

 

When I looked, I'd set up the metafield as a "list" instead of a single value.  So it WAS an array value after all.

 

Once I recreated the metafield as a "single value" field...altered the code to reflect the new value it worked.

 

#facepalm

 

So yeah.  Problem solved.  

View solution in original post

Reply 1 (1)

jknupp
Tourist
5 1 1

This is an accepted solution.

I just solved this.

 

The [value] lead me to think that it was returning an array value.

 

When I looked, I'd set up the metafield as a "list" instead of a single value.  So it WAS an array value after all.

 

Once I recreated the metafield as a "single value" field...altered the code to reflect the new value it worked.

 

#facepalm

 

So yeah.  Problem solved.