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
I think that your code is correct except one part
{% if tag contains customer.tags %}
this should be the other way around, like so: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.
There is no
product.tags
in the search result, it should beitem.tags