How can I exclude a specific Collection in "related-products.liquid"?

Rovin_Vinod
New Member
1 0 0

I'm trying to remove the "Related Products" on a Specific Collection Title.
I'm selling many products but I want one specific product to have it's own page.

I've been seeing some codes that this is the code, but I don't understand how to code it.

"{% assign exclusions = 'frontpage,all' | split: ',' %}"

I need help with my store.

0 Likes
Britton_Jenner
Shopify Staff (Retired)
Shopify Staff (Retired)
398 0 74

Hi Rovin,

Britton here with the Shopify support team.

I think I have found a way to exclude featured products from all products that are inside of a given collection. These sorts of fixes can be theme dependent, so if your featured products code is different from mine at all you will need to locate your version in your theme instead.

For the theme I was working with the featured products are added in product.liquid using this code:

{% if settings.related_products_enable %}
  {% assign grid_item_width = 'large--one-fifth medium--one-third small--one-half' %}
  {% include 'related-products' %}
{% endif %}

 

In order to hide this based on collection I changed that code to this:

{% assign collection_titles = product.collections | map: 'title' %}

{% unless collection_titles contains 'Test' %}
{% if settings.related_products_enable %}
  {% assign grid_item_width = 'large--one-fifth medium--one-third small--one-half' %}
  {% include 'related-products' %}
{% endif %}
{% endunless %}

The first line turns the array containing all the collections assigned to the product into a single string. The Unless statement then checks that string for the presence of your collection title (Test in my case). Using contains on product.collections without first using the map filter will not work in this case as the collections in product.collections are also arrays themselves. 

Make sure to change the 'Test' part of the code above to be the collection title that you are interested in and it may work for you. 

I hope that helps sort out your featured product woes!

-Britton

Britton Shopify Guru
0 Likes
aj007
Excursionist
31 0 59

Hi - the current 'related-products.liquid' code for the Pop them is pasted below.

 

How would I incorporate the exclusion handling stuff from https://gist.githubusercontent.com/sadiesaurus/8157cf791d6e8945254ec0240b840dd2/raw/294271eba8a42f25... into it?

 

My goal is to add more Collection exclusions to Pop in one list like this: 

{% assign exclusions = 'frontpage,all' | split: ',' %}

Rather than what Pop seems to do now (see below), which is use the clunky "...and c.handle != 'frontpage' and c.handle != 'all' and..." 

 

I'm not clear how the "{% unless exclusions contains..." bits would be incorporated in the following Pop code...

 - thanks, aj

 

 

{% assign number_of_products = 4 %}
{% assign product_size = 460 %}
{% assign number_of_products_to_fetch = number_of_products | plus: 1 %}

{% if collection == null or collection.handle == 'frontpage' or collection.handle == 'all' %}
  {% assign found_a_collection = false %}
  {% for c in product.collections %}
    {% if found_a_collection == false and c.handle != 'frontpage' and c.handle != 'all' and c.all_products_count > 1 %}
      {% assign found_a_collection = true %}
      {% assign collection = c %}
    {% endif %}
  {% endfor %}
{% endif %}

{% if collection and collection.products_count > 1 %}
  <div class="section-header text-center">
    <h2 class="section-header__title">{{ "products.general.related_products_title" | t }}</h2>
    <hr class="hr--small">
  </div>
  <div class="grid-uniform product-grid">
    {% assign current_product = product %}
    {% assign current_product_found = false %}
    {% assign grid_item_width = 'medium-down--one-half large--one-quarter' %}
    {% for product in collection.products limit: number_of_products_to_fetch %}
      {% if product.handle == current_product.handle %}
        {% assign current_product_found = true %}
      {% else %}
        {% unless current_product_found == false and forloop.last %}
          {% assign featured = product %}
          <div class="grid-item {{ grid_item_width }}">
            {% include 'product-grid-item' with product_size: product_size %}
          </div>
        {% endunless %}
      {% endif %}
    {% endfor %}
  </div>
{% endif %}
0 Likes
bredowmax
Explorer
57 1 67

I had the same question but the problem was more complex than necessary because a product can have many collections.

Since a product can have only 1 type, I decided for a simpler solution by excluding product recommendations based on product type, vs. excluding collection title. In my Canopy theme, this code in product.liquid works (assuming you want to exclude products with type 'hidden'):

{% unless product.type == 'hidden' %}
    {% section 'product-recommendations' %}
{% endunless %}

0 Likes