How to fetch unique value in liquid file - shopify

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/

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

An item in my array and a string have the same value, but why == evaluate to false?

I'm working on this code, which will tell if a product is shipped from the US.
{% assign USVendors = "
customcat,
gearment,
" %}
{% assign USVendorArray = USVendors | split: "," %}
{% assign ProductVendor = product.vendor | downcase | strip %}
{% assign USShipped = false %}
{% for item in USVendorArray %}
{% if item == ProductVendor %}
{% assign USShipped = true %}
{% break %}
{% endif %}
{% endfor %}
{{ USShipped }}
In one product in which Product.type = "CustomCat", USVendorArray[0] and ProductVedndor both equal customcat. And therefore, USShipped should be true, but it isn't.
I see the problem is in the if line, because when I change == to contain, it just works.
Can you please explain what is wrong with my code?

Go through array elements in Liquid

I'm writing some liquid code for a shopify website in order to add products to any blog post based on tags that use product's handle. I've tried with one product and it works just fine so I was trying to iterate it through a loop but I couldn't get any information from the array I've created. This is the code I've written so far. Can you help me understand what I'm doing wrong? Thanks!
{% comment %}declare variables{% endcomment %}
{% assign related_prod_index = 0 %}
{% assign related_prod_array = "" | split: ',' %}
{% comment %}check for tags that contains products handles{% endcomment %}
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
{% assign related_prod_array = related_prod_array | push:blog_prod %}
{% assign related_prod_index = related_prod_index | plus:1 %}
{% endif %}
{% endfor%}
{% comment %}check how many product tags I found{% endcomment %}
<h1>{{ related_prod_index }} tags found</h1>
{% comment %}loop that create small preview for each product{% endcomment %}
{% if related_prod_array %}
{% for rel_pr in related_prod_array %}
<img src="{{ rel_pr.featured_image | img_url:'original' }}">
<p>{{rel_pr.title}}</p>
<p>{{rel_pr.price | money}}</p>
{% endfor %}
{% endif %}
This looks like it should be working, but if this post is true, then you can't add Product objects to an array.
The good news is you shouldn't need to. Put the display code in the same loop you use to search through the tags.
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
<img src="{{ blog_prod .featured_image | img_url:'original' }}">
<p>{{blog_prod .title}}</p>
<p>{{blog_prod .price | money}}</p>
{% endif %}
{% endfor %}

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