Ad

Selecting Product Tag Based On Specific Pattern In Shopify Liquid Theme

- 1 answer

I'm new to Shopify Liquid Theme language and trying to find my way. I have the following products in my Shopify store with their tags:

CATEGORY CLOTHING
    t-shirt-A
     - Tags: t, -dog1-

    t-shirt-B
     - Tags: t, cat2

CATEGORY MUG
    mug-A
     - Tags: m, -dog1-

    mug-B
     - Tags: m, cat2

CATEGORY PHONECASE
    phonecase-A
     - Tags: p, -dog1-

    phonecase-B
     - Tags: p, cat2

From the above products you can see that t-shirt-A and t-shirt-B have similar tag "t", mug-A and mug-B have similar tag "m", etc...

You could also see that t-shirt-A, mug-A and Phonecase-A have a common second tag -dog1-. Though they belong to different collections/categories yet they have that -dog1- tag in addition to their specific first tags. I gave them additional -dog1- tag because they have the same dog image on them.

t-shirt-A, mug-A and Phonecase-A have the same dog image on them.

Now what I want to achieve is when a user is in the product detail page of t-shirt-A, I want to display other products (from different categories) which have the same image on them. In this case I want my liquid theme to display mug-A and Phonecase-A so that the user will see that the image on the t-shirt is also on the mug and the phonecase.

I have followed all the tutorials of Liquid and also the filters but I can't find any filter that can help me to select the tag of the current product with pattern matching like -[a-zA-Z]-.

The reason I say pattern matching is that other t-shirts, mugs, and phonecases are going to have -dog2- image on them.

So I think with pattern matching you can select a particular tag from the current product tag arrays that matches for instance -[a-zA-Z]- and when you have this particular tag you can compare it to all the tags in all the categories and select products that have the same tag.

Ad

Answer

Instead of looking for Regex options, use a custom tag prefix that will help you to determine the relevant tag.

tagPrefix:tagname

Doing so your input would become something like

CATEGORY CLOTHING
    t-shirt-A
     - Tags: t, sameImage:dogrefix:tagname

Doing so your input would become something like

CATEGORY CLOTHING
    t-shirt-A
     - Tags: t, sameImage:dog1

    t-shirt-B
     - Tags: t, sameImage:cat2

CATEGORY MUG
    mug-A
     - Tags: m, sameImage:dog1

    mug-B
     - Tags: m, sameImage:cat2

CATEGORY PHONECASE
    phonecase-A
     - Tags: p, sameImage:dog1

    phonecase-B
     - Tags: p, sameImage:cat2

Then inside your product template, first find the same image tag. For that you can use

{% for tag in product.tags %}
    {% if tag contains 'sameImage:' %}
      {% assign tagFound = tag %}
    {% endif %}
{% endfor %}

Once you have the value in tagFound variable you can iterate over other collections and use products with same tags.

However, to me the above method looks inefficient because of excessive loops on product collections. An alternate and much better approach will be to use product metafields to save informations of product handles and then use all_products to retrieve the linked products.

Shopify Metafields API

Shopify Metafields Object

Ad
source: stackoverflow.com
Ad