create array and get value in array Shopify - 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 %}

Related

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/

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?

Get number of available products in a Shopify collection

I realise I can use collection.products_count to get products count for a collection, but is it possible to get the count of available products, e.g. not sold?
You'll need to iterate over the products and check their availability:
{% assign available_products = 0 %}
{% for product in collection.products %}
{% if product.available %}
{% assign available_products = available_products | plus: 1 %}
{% endif %}
{% endfor %}
{{ available_products }}

How can I check for more than 1 product with same name?

I need to check if there are two or more products with the same name so I can add an if/else statement. So I'm thinking along the lines of:
{% if product.title == product.title %}
or
{% if product.title.size > 1 %}
But this doesn't seem to be right.
Thanks!
Are you looking for a specific product title, or any product title that is duplicated? If you are comparing against a given product title, perhaps try something like this:
{% assign found_duplicate = false %}
{% for p in products %}
{% if p.title == product.title %}
{% assign found_duplicate = true %}
{% endif %}
{% endfor %}
{% if found_duplicate == true %}
...
{% endif %}