Related Product By Tag and Collection

Highlighted
New Member
3 0 0

Hi all,

 

Looking to make this related product code work for tags. It works for collections at the minute but what we want it to do is to look for 3 products from the same tag (e.g 3 products from "black") and then one product from the same collection (e.g one product from "shirt") so if we had a black shirt, it would show 3 other black products and then one other shirt.

 

How would we modify the code to do this? The code is below...

 

{% assign number_of_products = 3 %}
{% assign number_of_products_to_fetch = number_of_products | plus: 1 %}

{% if tag == null or tag.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 %}
  <section class="related-products">
    <hr>
    <h2 class="section-header__title h3">{{ 'products.general.related_products_title' | t }}</h2>
    <div class="grid grid--uniform">
      {% assign current_product = product %}
      {% assign current_product_found = false %}
      {% 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 grid_item_width = 'medium-up--one-third small--one-half' %}
            {% include 'product-grid-item' %}
          {% endunless %}
        {% endif %}
      {% endfor %}
    </div>
  </section>
{% endif %}

Thank you!!

0 Likes
New Member
3 0 0

Thank you, however we want to be able to do it for free without the use of an app...

0 Likes