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
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?
Solved! Go to the solution
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!
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!
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
I'm not sure why its not separating them but it does remove the comma I guess..
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!
Wow this is perfect! Thank you so much.
User | RANK |
---|---|
41 | |
34 | |
13 | |
10 | |
8 |