I have an issue that seems to be fairly common, but I’ve not found a poster that is trying to go about solving it in the same way I am. Essentially, my client has a website that has some unisex products. And on the product and collection pages, they want to change the title and images of the product based on whether it is shown in the Men’s or Women’s collection. I’m choosing to tackle it like this:
-
Assign all unisex products the tag “unisex” plus “men” plus “women” so they’ll be added to a smart collection for Men’s and Women’s and so I can target the unisex products by that tag.
-
Add the word “Girls” or “Guys” to the ALT text string to hopefully sort them. I choose “girls” and “guys” since “men” is contained in the word “women”.
Now I can check if the product is tagged “unisex” and check if the word “mens” or “womens” appears in the collection URL to know what type of product page I’m on and if it’s an unisex product. And then can easily add the word “Men’s” or “Women’s” to the title. But the images are proving to be a bit tricky. I want to filter the array by only items containing my keywords (“guys” on Men’s collection and “girls” on Women’s). I know I can do a where filter, but that has to be an exact match. As in {% assign uni_media = product.media | where:“alt”, “Guys” %} . And that works, if my ALT text is exactly “Guys”. But I want to let my client write real SEO-friendly ALT tags, like “Guys Black Crew Neck T-Shirt”. So I researched Jekyll and Liquid a bit, and there’s something called where_exp, as in a where expression. So I could write a contains expression. I thought this must be my answer, however it’s not working. It just returns the full array no matter what parameters I use. Does anyone know if where_exp works in Shopify’s Liquid. And if so, maybe take a look at my logic/syntax and see if I’ve made a mistake? Or just give me a better way to go about getting my filtered array if I’m missing something obvious. I’d greatly appreciate it.
Here’s what I’m trying:
{%- if collection.url contains ‘/mens’ and product.tags contains ‘Unisex’ -%}
{%- assign uni_media = product.media | where_exp:“product.media”, “product.media.alt contains ‘Guys’” -%}
{%- elsif collection.url contains ‘/womens’ and product.tags contains ‘Unisex’ %}
{% assign uni_media = product.media | where_exp:“product.media”, “product.media.alt contains ‘Girls’” -%}
{%- else -%}
{%- assign uni_media = product.media | where: “media_type”, “image” -%}
{%- endif -%}
Then later I just use uni_media in place of product.media to get images.