Search code examples
shopifyliquidliquid-layout

Shopify search for related products is not returning similar tagged products


I have a custom module that returns the first 4 related products by tag. It works on ALMOST products except two tags and I can't figure out why. Excluding variants , there are only 108 products site wide. I am displaying the product tag right above the module for you to see as well.

Sample product page with working tag (PowerChairs) https://www.gallantmedicalsupply.com/collections/power-chairs/products/compass-sport-power-chair

Sample product page with non-working tag (Walkers) https://www.gallantmedicalsupply.com/products/walk_002

enter image description here

{% assign product = product %}
{% assign current_tags = product.tags %}
{% assign all_products = collections.all.products %}

<div  class="container custom-related-product">
  <section>
    <div>
      {% if section.settings.product_recommendations_heading != blank %}
        <div class="twelve{% unless settings.borders_enable %} bottompad{% endunless %}">
          <h2 class="text-center">{{ section.settings.product_recommendations_heading }}</h2>        
        
{% echo current_tags %}
        
        </div>
      {% endif %}
{% assign related_products = 0 %}    
      <div class="product-loop center">
          
        {% for similar_product in all_products %}
          {% if similar_product.handle != product.handle %}
            {% for current_product_tag in product.tags %}
              {% if similar_product.tags contains current_product_tag %}
                {%  render 'product.loop', product: similar_product, block_width: 'three' %}
{% assign related_products = related_products | plus: 1 %}
                {% break %}
              {% endif %}
            {% endfor %}
{% if related_products == 4 %}
{% break %}
{% endif %}
          {% endif %}
        {% endfor %}

        
      </div>
    </div>
  </section>
</div>

Solution

  • Due to the loop limit of 50 products. I filtered the all.products by collection first (excluding all collection and current product) and then iterating to render.

    {% if collection == null or collection.handle == 'frontpage' or collection.handle == 'all' %}
    {% assign found_a_collection = false %}
    {% for c in product.collections %}
      {% echo c %}
      {% if found_a_collection == false and c.handle != 'frontpage' and c.handle != 'all-products' and c.all_products_count > 1 %}
        {% assign found_a_collection = true %}
        {% assign collection = c %}
      {% endif %}
    {% endfor %}
    {% endif %}
    
    
    
      <div  class="container custom-related-product">
        <section>
               
          <div>
    
            {% if section.settings.product_recommendations_heading != blank %}
            <div class="twelve{% unless settings.borders_enable %} bottompad{% endunless %}">
              <h2 class="text-center">{{ section.settings.product_recommendations_heading }}</h2>
            </div>
            {% endif %}
    
            <div class="product-loop center">
            {% for similar_product in collection.products %}
              {% if similar_product.handle != product.handle %}
                {%  render 'product.loop', product: similar_product, block_width: 'three' %}
                {% assign related_products = related_products | plus: 1 %}
                {% if related_products == 4 %}
                  {% break %}
                {% endif %}
              {% endif %}
            {% endfor %}
            </div>
            
          </div>
    
        </section>
      </div>