PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 1, 2022

[FIXED] How to check if array item matches array item in liquid (Shopify)

 July 01, 2022     liquid, shopify     No comments   

Issue

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 %}

Solution

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 %}


Answered By - Charles C.
Answer Checked By - Dawn Plyler (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing