1
votes

For a client I'm building a way to choose what products are shown in the recommended products section on the product page. To achieve this I thought about inserting the handles from the products I'd like to display into the tag part of the product.

So I'm able to loop over all the tags from the product and then for every tag I'd take the product object and display it. The problem is this code doesn't seem to work:

{% for tag in tags %}
  {% for single_product in all_products[tag] %}
      {{ single_product }}
  {% endfor %}
{% endfor %}

This code does work:

{% for tag in tags %}
  {% for single_product in all_products[tag].title %} <---- Added .title
      {{ single_product }}
  {% endfor %}
{% endfor %}

Sadly, I need the entire product object to display it instead of only the product title. How do I achieve this in shopify?

Side info: This code is placed inside of the framework--product-recommendations.liquid file

1
I think you need to look into the collection.products, read moreOnkar
@Onkar Thanks! This worked perfectly!Lars van Erp
glad to know it helps you.Onkar

1 Answers

0
votes

The solution was using the collection instead of the all_product. I create a collection containing all product and was perfectly able to loop over it.

My code looks like this now:

          {% for tag in tags %}
            {% for single_product in all_the_products %}
                {% if tag == single_product.handle %}
                    <div class="product-recommendations--item">
                    {%
                    render 'framework--product--item',
                    product: single_product,
                    view: 'grid'
                    %}  
                    </div>
                {% endif %}
            {% endfor %}
          {% endfor %}

all_the_products is assigned to a collection containing all the products.