0
votes

On my product.liquid I am pulling my related-products.liquid segment of code:

{% if collection and collection.products_count > 1 %}

<div class="row">
  <div class="span12">
    <h2 class="collection-title">Related products</h2>
  </div>
</div> 

<div class="row products">

{% assign current_product = product %}
{% assign current_product_found = false %}
{% for product in collection.products limit: 5 %}
  {% if product.handle == current_product.handle %}
    {% assign current_product_found = true %}
  {% else %}
    {% unless current_product_found == false and forloop.last %}
      {% include 'product-loop-shop' with collection.handle %}
    {% endunless %}
  {% endif %}
{% endfor %}

</div>

{% endif %}

When I first click on a product from my home page the related products display, and when I click on one of the related products the whole related products block at bottom does not appear.

If I'm leaving out something please let me know.

1

1 Answers

1
votes

The collection variable is only set on the collection pages (pages using the collection.liquid template). Within the product pages (pages using the product.liquid template), it normally isn't set.

Nested product pages (/collection/sale/product/my_product) are the only exception where the collection variable is set within product pages (using the product.liquid).

So to solve your problem you can use two approaches:

  • use the within filter for all product urls inside the product-loop-shop, so the generated urls will always be nested within a collection
  • display the related products using the collections variable (note the extra s), which is a general variable and thus always set