skip to Main Content

Can anyone help me finish my code here? I’m trying to filter search results by only showing products tagged with a tag that matches customer tags.

For example, a customer (tagged with "Mustang") searches Ford on the front end.
(Normally – all Ford products would be displayed.)
However, If the customer is tagged Mustang, only products tagged Mustang would display.

Here’s what I have so far; the results aren’t empty, but they do not display.

{% for item in search.results %}
    {% if item.object_type == 'product' %}
 
        <!-- for tag in product tags do -->
        {% for tag in product.tags %}
 
            <!-- If tag in product tags do -->
            {% if tag contains customer.tags %}
                {% assign product = item %}
                {% include 'product-grid-item' %}
            {% endif %}
 
        {% endfor %} 

    {% else %}
        <div class="grid__item medium-up--one-third small--one-half">
            <h2 class="h3">{{ item.title | link_to: item.url }}</h2>
            <p>{{ item.content | strip_html | truncatewords: 50 }}</p>
        </div>
    {% endif %}
{% endfor %}

2

Answers


  1. I think that your code is correct except one part {% if tag contains customer.tags %} this should be the other way around, like so:

    {% if customer.tags contains tag %}
    

    You want to check if the customer.tags contains a specific tag and not that the tag contains the custom tags.

    This should fix your issue.

    Login or Signup to reply.
  2. There is no product.tags in the search result, it should be item.tags

    {% for item in search.results %}
        {% if item.object_type == 'product' %}
    
            <!-- for tag in product tags do -->
            {% for tag in item.tags %}
     
                <!-- If tag in product tags do -->
                {% if customer.tags contains tag %}
                    {% assign product = item %}
                    {% include 'product-grid-item' %}
                {% endif %}
     
            {% endfor %} 
    
        {% else %}
            <div class="grid__item medium-up--one-third small--one-half">
                <h2 class="h3">{{ item.title | link_to: item.url }}</h2>
                <p>{{ item.content | strip_html | truncatewords: 50 }}</p>
            </div>
        {% endif %}
    {% endfor %}
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search