For Loop not cycling trough products

Shopify Partner
24 0 0

I am trying to get a custom collection to show in a product page. I have setup some extra metafield so I can start selecting them and some custom grids. For some reason I keep getting the same product over and over. What I need it to do is get 1 product, then another one, etc.

 

Any help would be appreciated.

{% assign color = product.metafields.custom_fields["kleur"] %}

{% assign product_count = 0 %}
{% if product_count <= 7 %}
{% for product in current_collection limit: 8 %}
{% if product.metafields.custom_fields["kleur"] == color %}
{% assign product_name = product.title %}
{% assign product_img = product.featured_image.src | product_img_url: "50px" %}
{% assign product_price = product.price_min %}
{% assign product_link = product.url %}
{% assign product_count = product_count | plus: 1 %}
{% break %}
{% endif %}
{% endfor %}
{% endif %}

(I set a little counter to stop after 8 products with the product_count var)

0 Likes
Shopify Partner
452 78 85

@Stefan_W 

 

It loop only once, and when you are doing check for product_count, it can't understand, it is not within loop, it only runs once, and why you are doing break and limit: 8

Try this version.

{% assign color = product.metafields.custom_fields["kleur"] %}


{% for product in current_collection limit: 8 %}
{% if product.metafields.custom_fields["kleur"] == color %}
{% assign product_name = product.title %}
{% assign product_img = product.featured_image.src | product_img_url: "50px" %}
{% assign product_price = product.price_min %}
{% assign product_link = product.url %}
{% assign product_count = product_count | plus: 1 %}

{% endif %}
{% endfor %}





Author: Hovhannes Sargsyan (aka sarhov).
An individual web developer focused on Shopify development.
Need more help? Feel free to send me a direct message or drop an email shopify@sarhov.com

Website: https://sarhov.com  
Email: shopify@sarhov.com
Skype: @sarhov
Messenger: @sarhov
Telegram: @sarhov
0 Likes
Shopify Partner
24 0 0

Thank you for the suggestion. I tried but that's not yet the answer.

I need it to cycle trough 8 different products from the whole collection.

 

I did modify it to be shorter

	{% for product in current_collection limit: 8 %}
		{% if product.metafields.custom_fields["kleur"] == color %}
			{% assign product_name = product.title %}
			{% assign product_img = product.featured_image.src | product_img_url: "50px" %}
			{% assign product_price = product.price_min %}
			{% assign product_link = product.url %}
		{% endif %}
		{% include 'sw-productmatch-list' %}
	{% endfor %}

I need something like a 'product[0], product[1], etc but then usefull in Liquid.

0 Likes
Highlighted
Shopify Partner
452 78 85

@Stefan_W 

 

I see, but are you sure that the first 8 products in whole collection will have the correct value, because when you limit for 8 loops, it will not go further.

Author: Hovhannes Sargsyan (aka sarhov).
An individual web developer focused on Shopify development.
Need more help? Feel free to send me a direct message or drop an email shopify@sarhov.com

Website: https://sarhov.com  
Email: shopify@sarhov.com
Skype: @sarhov
Messenger: @sarhov
Telegram: @sarhov
0 Likes
Shopify Partner
24 0 0

I can sort them with price, popularity and other options. This is not the final version of the setup, there are more variants. But the core is getting the complete selection and then being able to apply some deeper selections.

0 Likes
Shopify Partner
24 0 0

I found a solution now. It's on a different way.

I still would like to know how to manually pull products so I don't get the 'ProductDrop', but for now I can continue.

I will leave this open for a bit longer because someone might know the answer to the original question.

0 Likes