Exluding the existing product from collection section in product page

Hey everyone,

I have a collection snippet that I would like to add to my product page, and I actually can add it there. However, the collection section also shows the product that is in the current product page. I would like to exclude the products that are on view from collection section. What kind of a conditional should I use for that?

anyone?

Hey @batulkr

Most likely you include the snippet like this:

{% render 'your-snippet' %}

or

{% include 'your-snippet' %}

First, you need to pass the current product object:

{% render 'your-snippet', current_product: product %}

Within your snippet most likely you have a loop through products. You need to compare handles of the products:

{% for product in all_products %}
  {% unless product.handle == current_product.handle %}
    ...

  {% endunless %}
{% endfor %}

thanks so much!

1 Like