What's your biggest current challenge? Have your say in Community Polls along the right column.

How can I correctly create an array and add items in code?

How can I correctly create an array and add items in code?

archimed
Tourist
11 0 4

Hi everyone,

I'm writing some code 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><a href="{{ rel_pr.url}}">{{rel_pr.title}}</a></p>
        <p><a href="{{ rel_pr.url}}">{{rel_pr.price | money}}</a></p>
      {% endfor %}
{% endif %}  
Replies 0 (0)