We're moving the community! Starting July 7, the current community will be read-only for approx. 2 weeks. You can browse content, but posting will be temporarily unavailable. Learn more

Itterating variable name in loop to access values.

Itterating variable name in loop to access values.

pepsimax2k
Shopify Partner
15 0 1

Hey all, I've seen similar questions to this but I have a catch - my loop index needs modifying.

 

I have a query returning values for "bundle_child_01" through "bundle_child_10", and instead of 10 if statements, thought I'd try a single for loop... 

 

 

 

 

{% for i in (1..10) %}
				
  {% capture var %} variant.bundle_child_{{ i | prepend: '00' | slice: -2, 2 }}.value {% endcapture %}

 

 

 

 
What am I doing wrong? No amount of checking [var] or var is returning anything but nulls or just the variable name, not it's value. 

 

Weirdly, this works in theory: 

 

 

 

{% assign child_02 = "gid://tewtewt" %}
{% for i in (1..10) %}
  {% capture var %}child_{{ i | prepend: '00' | slice: -2, 2 }}{% endcapture %}
  {% if [var] %}
      "{{ [var] }}"
  {% endif %}
{%- endfor %}

 

 

 


In this case, [var] is gid://tewtewt. So what gives? 

 

 

 

EDIT: Gave up. Did the below instead, much simpler 😛

 

			{% assign bundle_child_array = array %}
			{% assign bundle_child_array[0] = variant.bundle_child_01.value %}
			{% assign bundle_child_array[1] = variant.bundle_child_02.value %}
			{% assign bundle_child_array[2] = variant.bundle_child_03.value %}
			{% assign bundle_child_array[3] = variant.bundle_child_04.value %}
			{% assign bundle_child_array[4] = variant.bundle_child_05.value %}
			{% assign bundle_child_array[5] = variant.bundle_child_06.value %}
			{% assign bundle_child_array[6] = variant.bundle_child_07.value %}
			{% assign bundle_child_array[7] = variant.bundle_child_08.value %}
			{% assign bundle_child_array[8] = variant.bundle_child_09.value %}
			{% assign bundle_child_array[9] = variant.bundle_child_10.value %}

			{% for bundle_child in bundle_child_array %}
Replies 2 (2)

Small_Task_Help
Shopify Partner
1144 55 112

Hi,

 

If your variables (e.g., bundle_child_01, bundle_child_02, etc.) are stored as an array or a collection, it’s much easier to loop through them.

To Get Shopify Experts Help, Click Here or E-mail - hi@ecommercesmalltask.com
About Us - We are Shopify Developers India
At Google My Business - Ecommerce Small Task - Hire Shopify Developers Ahmedabad
pepsimax2k
Shopify Partner
15 0 1

Unfortunately they’re individual product references, as I ultimately need them as keyval pairs (reference, quantity) but no easy support for this in metafields (there’s mixed ref list but I don’t fancy spending a month learning how to use it 😋 looping through the individual values above took me a whole day to figure out as it is 😭).