RamanSall
RamanSall

Reputation: 385

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.

Upvotes: 1

Views: 8670

Answers (2)

miglio
miglio

Reputation: 2058

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

Upvotes: 5

bknights
bknights

Reputation: 15377

Are you trying to create an array of product ids?

You could do that like:

{% assign productids = collections.frontpage.products | map: 'id' %}
{{ productids |join: ','}}

Upvotes: 4

Related Questions