problem when comparing product.id with value inside an array (with product.id's)

Highlighted
Shopify Partner
3 0 0

Hi there, 

I'm trying to build a solution where I have some arrays with product.id's and I loop through these arrays to make an specific output for products that are inside this array. I'm being able to build the array, the array keeps the product.id's values (I tested to ouput its items) but at the time when I do the loop and compare with values inside the array it doesn't work.

Someone have some guess? 

My code for the comparison is:

    {% for product in collections.venuetest.products %}
      {% if allmatchedcount contains product.id %}
        {% include 'venuesolution' %}
      {% endif %}

      {% if fivematchedcount contains product.id %}
        {% include 'venuesolution' %}
      {% endif %}

      {% if fourmatchedcount contains product.id %}
        {% include 'venuesolution' %}
      {% endif %}
    {% endfor %} 

I also tried like this: 

    {% for product in collections.venuetest.products %}
      {% for x in allmatchedcount %}
        {% if x == product.id %}
          {% include 'venuesolution' %}
        {% endif %}
      {% endfor %}
  
      {% for x in fivematchedcount %}
        {% if x == product.id %}
          {% include 'venuesolution' %}
        {% endif %}
      {% endfor %}
      
      {% for x in threematchedcount %}
        {% if x == product.id %}
          {% include 'venuesolution' %}
        {% endif %}
      {% endfor %}
    {% endfor %}

0 Likes
Highlighted
Shopify Partner
385 0 50

Hello dbranco,

What are exactly you trying to do?

How are you producing the allmatchedcount, fivematchedcount, fourmatchedcount?

Please note if you actually creating proper arrays and not strings contains will not work unless its actually a string.

Kasimir

"Good design is good business"
0 Likes
Highlighted
Shopify Partner
3 0 0

Hello Kasimir, 

Thanks for the reply.

I'm trying to retrieve the value for specific products.id's, put them in specific arrays, and after do 1 for loop for products to output the products whose product.id's are inside these arrays. 

I'm doing an empty array with this: 

{% assign allmatchedcount     = ' ' | split: ',' %}
{% assign fivematchedcount     = ' ' | split: ',' %}
{% assign fourmatchedcount     = ' ' | split: ',' %}

And I do 1 forloop for products creating in the beginning of the loop another array with 1 element: 

{% for product in collections.venuetest.products %}

  {% assign prod_id = product.id | split:',' %}

And I add the product id of specific products for one of the arrays for a code like that: 

  {% assign allmatchedcount = allmatchedcount | concat: prod_id %}

{% endfor %}

When I output the values inside the array it shows me the product.id's, but it doesn't work for the comparisson. 

0 Likes
Highlighted
Shopify Partner
385 0 50

Hello dbranco,

Thanks for the clarification.

This example does not make much sense as you could do this in one loop, but to explain it lets assume we wanted to get all the product ids of products that price is 185 

{% assign compaire = '' %}

{% for product in collection.products  %}
  {% if product.price == 18500 %}
    {% capture compaire %}
      {{ compaire }} {{ product.id }}
    {% endcapture %}
  {% endif %}
{% endfor %}

This would get the product ids and make it into a large string, which then would allow you to compaire them in loops easily.

To output we are getting the correct ids, use:

{{ compaire }}

Then we can use this "list of products" in a product loop to get only the products that match the id:

{% for product in collection.products  %}
  {% if compaire contains product.id %}
    {{ product.title }}
  {% endif %}
{% endfor %}

You can obviously pass the product to your own snippet aswell:

{% for product in collection.products  %}
  {% if compaire contains product.id %}
    {% include 'product-item' %}
  {% endif %}
{% endfor %}

Does this make sense for you? Obviously you need to modify this to what ever you actually want to get.

Let me know if you need additional guidance.

Kasimir

"Good design is good business"
0 Likes
Highlighted
Shopify Partner
3 0 0

Hello Kasimir, 

 

That makes totally sense, I didn't know this technique (just 1 month with shopify and liquid code). 

Thanks a lot. 

 

0 Likes