Shopify creates a URL to each collection filter. This URL https://example.com/collections/default/type_tag+size_tag+width_tag would filter products on a collection called default to show products that are tagged with type_tag, size_tag, width_tag.
This also creates a canonical link with the same href as the above URL. In this example, the canonical href would be <link rel="canonical" href="https://example.com/collections/default/type_tag+size_tag+width_tag">
We want to optimize for SEO. For URLs with the same filters but inverse (and therefore yield the same page and filtered content) we would like to have the same canonical href.
For example: These URLs would filter the same products on collections default and show canonicals with matching href value.
https://example.com/collections/default/type_tag+size_tag+width_tag
https://example.com/collections/default/size_tag+width_tag+type_tag
https://example.com/collections/default/type_tag+width_tag+size_tag
https://example.com/collections/default/width_tag+type_tag+size_tag
Regardless of the URLs above, we would like the canonical href to be fixed as the one we set. So as an example, we want the above URLs to have https://example.com/collections/default/type_tag+size_tag+width_tag as the canonical.
As you can tell, our tags are grouped into type_, size_ and width_.
I have tried several methods to loop over the {% current_tags %} object to check if any of the tags in {% current_tags %} contains type_, size_ and width_. Below is the idea that I have but clearly it doesn't work so it's trash. Fresh ideas please and thank you!!!
{% if template contains 'collection' and current_tags %}
{% for tag in current_tags %}
{% if current_tags contains 'type_' %}
{% assign type_tag_url = tag %}
{% elsif current_tags contains 'size_' %}
{% assign size_tag_url = tag %}
{% elsif current_tags contains 'width_' %}
{% assign width_tag_url = tag %}
{% endif %}
{% endfor %}
<link rel="canonical" href="{{ shop.url }}{{ collection.url }}/{{type_tag_url}}+{{size_tag_url}}+{{width_tag_url}}" />
{% else %}
<link rel="canonical" href="{{ canonical_url }}">
{% endif %}