dynamically calling liquid commands - shopify

I am trying to call the metafields of an array of value by "injecting" it into the command, is something like this possible? I have tried the below but it will not return a value.
Also, i have noted that i would require the page to be reloaded when a variant is selected is this possible?
{% assign main_dimensions = "width_cm, height_cm, depth_cm, length_cm" | split: ','%}
{% assign main_dimensions_title = "Width (cm), Height (cm), Depth (cm), Length(cm)" | split: ','%}
{% for i in main_dimensions %}
{{ main_dimensions_title[1]}} - {{ product.metafields.global.main_dimensions[1].value }}
{% endfor %}

After replicating the same on my dev store, I found some weired that need to be removed then It will worked as desired.
{% assign main_dimensions = "width_cm, height_cm, depth_cm, length_cm" | split: ',' %}
{% assign main_dimensions_title = "Width (cm), Height (cm), Depth (cm), Length(cm)" | split: ',' %}
{% for i in main_dimensions %}
// when split it it adding the space infront of second element due to space before , in to string
{% assign key = i | remove: ' ' %}
{{ main_dimensions_title[forloop.index0]}} - {{ product.metafields.global.main_dimensions[key].value }} <br/>
{% endfor %}
so use remove to eliminate it or simply create the string like this bellow without spaces.
{% assign main_dimensions = "width_cm,height_cm,depth_cm,length_cm" | split: ',' %}
{% assign main_dimensions_title = "Width (cm), Height (cm), Depth (cm), Length(cm)" | split: ',' %}
{{ product.metafields.global | json }}<br/>
{% for i in main_dimensions %}
{{ main_dimensions_title[forloop.index0]}} - {{ product.metafields.global.main_dimensions[i].value }} <br/>
{% endfor %}

Related

Go through all available variants in Shopify liquid

I am trying to show all the available sizes from a product in the collection page.
I managed to write this snippet that will show available sizes, but only for one of the color combinations.
{% for option in product.options_with_values %}
{% assign downcased_option = option.name | downcase %}
{% if downcased_option contains 'size' %}
{% assign is_size = true %}
{% for value in option.values %}
{% assign variant_available = true %}
{% if product.options.size >= 1 %}
{% unless product.variants[forloop.index0].available %}
{% assign variant_available = false %}
{% endunless %}
{% endif %}
<span class="{% unless variant_available %}soldout{% endunless %}">{{ value | escape }}</span>
{% endfor %}
{% endif %}
{% endfor %}
But, let's say we have the next variants:
Green/32, Green/34
Blue/34
Black/34, Black/36
With the snippet above, it will not show available size 36 from black color as it's not available in green.
What I want is to go through every single size and show if availability, no matter the color.
Does someone know how to achieve this?
Thanks
Forgive me if I'm missing something but making the assumption that all variants have a size and there are only two options you just need to loop through the available variants and just display the variant name split it and display first or last where ever the size is in the options list. if there are loads of duplicates then put them in an array and run 'uniq'.
{% for variant in product.variants %}
{% if variant.available %}
{% assign sizesList = variant.title | split: '/' | first | append: ', ' }}
{% endif %}
{% endfor %}
{% assign sizes = sizesList | split: ', ' %}
{{ sizes | uniq | join: ", " }}

how to get highest number of loop elements in shopify

how can I get highest numbers from loop here is my code
from this code I got 2,1,3,1,2
So I want 3 as a result could you please help me to solve this problem any help would be appriciated
{% for line_item in cart.items %}
{% assign c_no = line_item.properties | join:',' | remove_first : 'meal pack,' | strip | split : '-' | last | sort: 'price' %}
<h1>{{c_no}}</h1>
{% endfor %}
Use the {{ forloop.length }}
{% for product in collections.frontpage.products %}
{% if forloop.first %}
<p>This collection has {{ forloop.length }} products:</p>
{% endif %}
<p>{{ product.title }}</p>
{% endfor %}
Docs: https://shopify.dev/api/liquid/objects/for-loops

How to fetch unique value in liquid file

here is my code i tried to fetch only unique value from items variable but I got all value can someone please help to solve this
{% capture items %}
{% for tag in product.tags %}
{%if tag contains 'pair'%}
{% assign tag_split = tag | remove_first: 'pair::' | split: "::" %}
{% assign color = tag_split[0] %}
{{color}}
{% endif %}
{% endfor %}
{% endcapture %}
{% assign my_array = items | split: ", " %}
<p>{{ my_array | uniq }} </p>
This filter should help you to keep only one elem per value:
https://shopify.github.io/liquid/filters/uniq/

Unable to Concat two values in Shopify LIquid

I am trying to concat the variables together in shopify liquid, but I am unable to get it done
Here is my snippet
{% for product in collections.brands.products %}
{% assign productlist = product.title %}
{{ productlist | join: ', ' }}
{% endfor %}
{% for product in collections.clothing.products %}
{% assign productlists = product.title | join: ', ' %}
{{ productlists | join: ', ' }}
{% endfor %}
{% assign allcombine = productlist | concat: productlists %}
{{ allcombine }}
First at all you have to concat then print it
{% assign productlist = product.title | append: ',' %} // concat
{{ productlist }} // print

How to hide a tag on a Shopify product page if certain condition is met?

A liquid logic is calculating the highest score (tag) and outputting the result on the product page. However, the same list of scores (tags) is also being displayed somewhere else on the page. I'd like to be able to hide the highest score in the list of scores as this is already showing in a separate box.
Here is my logic calculating the highest score:
{%- capture list_of_scores -%}
{{wa}}|Wine Advocate,
{{bh}}|Burghound,
{{ag}}|Vinous,
{{jr}}|Jancis Robinson,
{{jg}}|John Gilman
{%- endcapture -%}
{%- capture list_of_scores_num -%}
{{wa}},
{{bh}},
{{ag}},
{{jr}},
{{jg}}
{%- endcapture -%}
{% assign scores_array = list_of_scores | split: ',' %}
{% assign scores = list_of_scores_num | split: ',' %}
{% assign highest_score = scores | first | plus: 0 %}
{% assign name = '' %}
{% for score_val in scores %}
{% assign cur_score = score_val | plus: 0 %}
{% if cur_score >= highest_score %}
{% assign highest_score = score_val | plus: 0 %}
{% assign name = scores_array[forloop.index0] | split: '|' | last %}
{% endif %}
{% endfor %}
<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>
I tried this to hide the highest tag within the list:
{% if wa != highest_score %}<span>WA {{ wa }}</span>{% endif %}
Any suggestions?
{% if wa != highest_score %} this condition will always be true as it seems that wa is a string and highest_score is an integer (as | plus: 0 is used to cast a string to an integer). An integer cannot be equal to a string.
You can try to also cast wa to an integer and then compare it to highest_score :
{% assign waToInt = wa | plus: 0 %}
{% if waToInt != highest_score %}<span>WA {{ wa }}</span>{% endif %}