Get all products by product tag?

Shopify Partner
4 0 0

Hi,

I'm trying to display all products with the same tag.

Found the tag by:

  {% for tag in product.tags %}
  {% if tag contains 'variant-' %}
  {% assign variant_tag = tag %}
  {% break %}
  {% endif %}
  {% endfor %}

Then loop throug products to find the ones with same tag:

  {% for product in collections[product.type].products %}
  {% unless product.handle == current_product.handle %}
  {% if product.tags contains variant_tag %}
  <div class="variant-grid__item {{ grid_item_width }}">
    <a class="shop-now" href="{{ product.url }}"><img src="{{ product.featured_image.src | img_url: 'small', scale: 2 }}" alt="{{ product.featured_image.alt | escape }}"></a>
  </div>
  {% endif %}
  {% endunless %}
  {% endfor %}

The above works, if I have a collection with the same name as the product type.

But I would like not to be dependent on this :)

I think I would like to loop through all products, and have tried this:

{% for product in collections.all.products %}
  {% unless product.handle == current_product.handle %}
  {% if product.tags contains variant_tag %}
  <div class="variant-grid__item {{ grid_item_width }}">
    <a class="shop-now" href="{{ product.url }}"><img src="{{ product.featured_image.src | img_url: 'small', scale: 2 }}" alt="{{ product.featured_image.alt | escape }}"></a>
  </div>
  {% endif %}
  {% endunless %}
  {% endfor %}

Changed collections[product.type] to collections.all, but it does not work.

Can anyone help?

Thanks.

0 Likes
Highlighted
Shopify Partner
4 0 0

Is this not possible?

0 Likes