Pregunta

I am trying to add breadcrumbs for my product pages on an ecommerce store. I need some help with the liquid syntax.

I want to include the first product tag as a part of the breadcrumb, unless the tag is 'stickers', 'stationery', or 'accessories', in which case I would like to use the second tag. If the second tag is also either 'stickers', 'stationery', or 'accessories', I would like to use the third tag, and so on.

Perhaps a better way to say this would be: I would like to call the first available tag that isn't either 'stickers', 'stationery', or 'accessories'.

The closest I have gotten is this:

{% if product.tags.first contains 'stickers' or product.tags.first contains 'stationery' or product.tags.first contains 'accessories' %}
 <li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/collections/all/{{ product.tags.last }}" itemprop="url"><span itemprop="title">{% if product.tags.size > 0 %}{% assign words = product.tags.last | split: '-' %}{% for word in words %}{% if word == 'and' %}{{ word }} {% else %}{{ word | capitalize }} {% endif %} {% endfor %}{% endif %}</span></a></li>
{% else %}  
 <li itemscope itemtype="http://data-vocabulary.org/Breadcrumb"><a href="/collections/all/{{ product.tags.first }}" itemprop="url"><span itemprop="title">{% if product.tags.size > 0 %}{% assign words = product.tags.first | split: '-' %}{% for word in words %}{% if word == 'and' %}{{ word }} {% else %}{{ word | capitalize }} {% endif %} {% endfor %}{% endif %}</span></a></li>
{% endif %}

This is inelegant, but it works up until a point. The point at which is breaks down is when both the first and last tags fall into one of the categories I wanted to exclude.

As far as I can see, there doesn't seem to be an option to call something like "product.tags.second" (only "first" and "last" seem to work).

I am a little out of my depth and would really appreciate some advice on how to sort this out.

The platform I'm on is Shopify.

Thanks!

¿Fue útil?

Solución

Here's a couple of ways you could approach it:

<!-- 1. Loop through the product tags to find the first tag you want included in the breadcrumb -->
{% assign found_tag = false %}
{% assign tag_for_breadcrumb = '' %}

{% for tag in product.tags %}
  {% if found_tag == false and tag != 'stickers' and tag != 'stationery' and tag != 'accessories' %}
    {% assign tag_for_breadcrumb = tag %}
    {% assign found_tag = true %}
  {% endif %}
{% endfor %}

{{ tag_for_breadcrumb }}

<!-- 2. Convert the tags array into a string, remove the tags you don't want, and then get the first tag from those remaining -->
{% assign tag_string = product.tags | join: ' ' %}
{% assign filtered_tag_string = tag_string | remove: 'stickers' | remove: 'stationery' | remove: 'accessories' %}
{% assign filtered_tag_array = filtered_tag_string | split: ' ' %}
{{ filtered_tag_array.first }}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top