Show Content For More Than 1 Collection

user73943
Tourist
4 1 0

Hi everyone,

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 %}
  ** 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 %}
  ** 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?

0 Likes
oliversinz
Shopify Partner
23 4 5

Hi @user73943 

Welcome to Shopify Community!

product.collections
Returns an array of all of the collections that a product belongs to, except any collections that are not available on the sales channel being used.

collection.products

Returns all of the products in a collection. You can show a maximum of 50 products per page.

To get pull products from collections that are associated with the product on its product page:

 

{% comment %}
    fetch all the collections related to your product
{% endcomment %}

{% assign related_collections = product.collections %}

{% for collection in related_collections %}

    {% comment %}
        fetch all the products in the current collection in the first loop.
    {% endcomment %}

    {% assign products = collection.products %}
    {% for product in products  %}
        ** Products **
    {% endfor %}
{% endfor %}

 

  

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
user73943
Tourist
4 1 0

Hi @oliversinz

Thank you. It seems to cause an error when setting a limit to 6 items for instance it ends up pulling 6 items of each of the collections and producing duplicates of products that show up on multiple collections.

Do you know a way around this?

0 Likes
oliversinz
Shopify Partner
23 4 5

Sure. You're welcome!

Let me check it out and get back to you.

- Was my reply helpful? Click Like to let me know!
- Was your question answered? Mark it as an Accepted Solution
0 Likes