TheChasers
TheChasers

Reputation: 65

Pulling Products From Multiple Collections - Shopify Liquid

I am having trouble trying to pull products from more than one collections that are associated with the product on its product page. I have coded the following:

{% for product in product.collections limit: 6 %}
  ** Products **
{% endfor %}

However, this just pulls all the list of collections that's associated with the product rather than the products in those collections. I have then tried the following:

{% for product in collections[product.collections].products limit: 6 %}
  ** Products **
{% endfor %}

Which came back with an "Liquid error: Expected handle to be a String but got Array" error message.

I am not sure how I should approach this. Does anybody know where I've gone wrong?

Upvotes: 1

Views: 1440

Answers (1)

Alice Girard
Alice Girard

Reputation: 2173

You are going to fast. Collections is an array so you must first define which collection you need before its products.

So, for example if you want only products from first collection of current product you might try this to access the collection itself inside product.collections array:

 {% for product in product.collections.first.products %}
     Do something with product object.
 {% endfor %}

Then, regarding what you are trying to achieve, first loop through product.collections, then loop through each collection products. Like this:

{% for collection in product.collections %}
    {% for product in collection.products limit:6 %}
        Do something
   {% endfor %}
{% endfor %}

Please note that double loops are greedy and might affect page load time. So you might need to limit primary loop too to avoid unnecessary load if product belongs to lot of collections.

Upvotes: 2

Related Questions