Trying to add custom code that splits a metafield string into an array

Solved
Frustration
Tourist
3 0 1

I cant sort of get it working with this online code I found 

 

 

<select class="select-year-parts select-year-parts-result">
{%- assign icons = "one, two, three" | split:", " -%}

    {% for i in icons %}
  <option value="{{ i }}"></option>
    {% endfor %}
</select>

 

 

which successfully splits the simple string
Screenshot 2022-03-22 162659.png
Okay so now the problem.. the string I'm trying to split is a metafield i created "product.metafields.year_n.year_k.value" and I can't find any way to basically swap the plain text string "One, Two, Three" with the metafield string

 

 

<select class="select-year-parts select-year-parts-result">
{%- assign icons = product.metafields.year_n.year_k.value | split:", " -%}

    {% for i in icons %}
  <option value="{{ i }}"></option>
    {% endfor %}
</select>

 

 

Why doesn't this work? 


Accepted Solution (1)

Accepted Solutions
LitExtension
Shopify Partner
3362 664 725

This is an accepted solution.

Hi @Frustration,

Please change code:

<select class="select-year-parts select-year-parts-result">
  {%- for product in collection.products -%}
	{%- if product.metafields.year_n.year_k.value != blank -%}
		{%- assign years = product.metafields.year_n.year_k.value | split: ", " -%}
		{% for year in years %}
			<option value="{{ year }}">{{ year }}</option>
		{% endfor %}
	{%- endif -%}
  {%- endfor -%}
</select>

Hope it helps!

LitExtension - #1 Shopping Cart Migration Expert
If our answer is helpful then please accept as solution!
Are you looking for a solution to migrate to Shopify? Contact us for further assistance.
Try our FREE DEMO now!

View solution in original post

Replies 4 (4)
LitExtension
Shopify Partner
3362 664 725

Hi @Frustration,

Variable 'product.metafields.year_n.year_k' has a value of a string separated by ', '?

Can you send me the value of this: {{ product.metafields.year_n.year_k.value }}

Hope it helps!

LitExtension - #1 Shopping Cart Migration Expert
If our answer is helpful then please accept as solution!
Are you looking for a solution to migrate to Shopify? Contact us for further assistance.
Try our FREE DEMO now!
Frustration
Tourist
3 0 1

Thank you for the reply! 
To give a bit more detail I'm setting up a search using metafields. A lot of my products have a year range so I was hoping I could add something like "2001, 2002, 2003, 2004, 2005" in the metafield box on the backend product page and this code would take that string and separate them into individual values on a drop down menu. 

This is current code 

<select class="select-year-parts select-year-parts-result">
  {%- for product in collection.products -%}
    <option value="{{ product.metafields.year_n.year_k.value | split: ", " }}">{{ product.metafields.year_n.year_k.value }}</option>
  {%- endfor -%}
</select>

And what it does 
Screenshot 2022-03-23 081655.png

I'm not sure why its not separating them but it does remove the comma I guess..

LitExtension
Shopify Partner
3362 664 725

This is an accepted solution.

Hi @Frustration,

Please change code:

<select class="select-year-parts select-year-parts-result">
  {%- for product in collection.products -%}
	{%- if product.metafields.year_n.year_k.value != blank -%}
		{%- assign years = product.metafields.year_n.year_k.value | split: ", " -%}
		{% for year in years %}
			<option value="{{ year }}">{{ year }}</option>
		{% endfor %}
	{%- endif -%}
  {%- endfor -%}
</select>

Hope it helps!

LitExtension - #1 Shopping Cart Migration Expert
If our answer is helpful then please accept as solution!
Are you looking for a solution to migrate to Shopify? Contact us for further assistance.
Try our FREE DEMO now!
Frustration
Tourist
3 0 1

Wow this is perfect! Thank you so much.