Shopify Products' Collection Empty In Collection Context

- 1 answer

I'm trying to filter my related products by collections.

Every product belongs to two collections: One for the material, one for the room.

The Material one is an automatic collection which get products with a certain tag.

The Room one is populated manually.

When I get to the product page I load the related products like this (the related product must share both collections with the current product) :

{% for related_product in collection.products %}

{% if product.collections[0].handle == 
related_product.collections[0].handle and product.collections[1].handle 
== related_product.collections[1].handle and related_product.handle != 
product.handle %}

<div class="Carousel__Cell">
  {% include 'product-item', product: related_product, 
   section.settings.show_product_info, show_labels: true %}

{% endif %}

{% endfor %}

Strangely this work only for some products. In some of them product.collection seems null, which makes no sense!

All of the products appear properly in the correct collection.

The problem is happening in the context of a collection (but we will need to make it work in all contexts)



We found out it was an import/sync problem. We had to remove all existing products and re-import them. It fixed the issue.