Unable to Concat two values in Shopify LIquid - shopify

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

Related

dynamically calling liquid commands

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

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/

How to output highest score and name associated with it - liquid logic

I need to output the highest_score and the name associated with it. The code outputs the highest_score but it's not associated with the right name. I guess it needs a for within a for loop, but I'm not quite sure how to construct it.
Here's my code:
{%- 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 %}
{% for score_val in scores %}
{% assign cur_score = score_val | plus: 0 %}
{% if cur_score >= highest_score %}
{% assign highest_score = score_val | plus: 0 %}
{% endif %}
{% endfor %}
{% for score_and_name in scores_array %}
{% assign split_score_and_name = score_and_name | split: '|' %}
{% assign score = split_score_and_name[0] %}
{% assign score = highest_score %}
{% assign name = split_score_and_name[1] %}
{% endfor %}
<span>{{ highest_score }}</span>
<h5>{{ name }}</h5>
Thanks
You can do something like this
{% assign wa = 12 %}
{% assign bh = 16 %}
{% assign ag = 26 %}
{% assign jr = 6 %}
{% assign jg = 11 %}
{%- 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>
The main difference is that we moved the name value outside of the loop as an empty variable and inside the loop that we check the highest number we assign the name variable using the forloop.index0 using the following code:
{% assign name = scores_array[forloop.index0] | split: '|' | last %}
So we need only 1 loop.

create array and get value in array Shopify

I want to create array and which I am creating in loop but I want that array to be declared out of the loop.This to be done in shopify: I am using:
{% assign productid="" %}
{% for product in collections.frontpage.products %}
{% assign product = product.id | split: ", " %}
{% endfor %}
{{product}} // should return value 3,4,4 but not returning
My explanation is not good But I tried my best to explain .Please can any one help me in this.
Are you trying to create an array of product ids?
You could do that like:
{% assign productids = collections.frontpage.products | map: 'id' %}
{{ productids |join: ','}}
you have 2 ways to get that:
1:
{% assign productids = "" %}
{% for product in collections.frontpage.products %}
{% assign productids = productids | append: product.id | append: ',' %}
{% endfor %}
<p>{{ productids }}</p>
...
2:
{% assign productids = collections.frontpage.products | map: 'id' %}
{% for pid in productids %}
<p>{{ pid }}</p>
{% endfor %}