MikesBarto2002
MikesBarto2002

Reputation: 173

Filter products through tags in Shopify

I am trying to pull only the products from a collection that have a certain tag. This is my way of creating sub-collections since Shopify doesn't do this. My code doesn't seem to be working, and I can't figure out why it isn't working. I am doing this on the collection.liquid page. It is just printing the headings to the screen, but not the list of products. Any ideas?

{% if collection.handle == "all" %}
  <!-- All Collections -->
    <div id="collections">
      <h2>Brave Bracelets</h2>
      <div class="product-list clearfix">
        <h3>Cerulean</h3>
        <ul>
          {% for product in collections.brave-bracelets.products %}
            {% capture alt_attr %}{{ product.title }} by The Brave Collection{% endcapture %}
            {% if product.tags contains "cerulean" %}
              <li>
                <a href="{{ product.url }}"><img src="{{ product.featured_image | product_img_url: 'compact' }}" alt="{{ alt_attr }}" /></a>
                <h3><a href="{{ product.url }}">{{ product.title }}</a></h3>
              </li>
            {% endif %}
          {% endfor %}
        </ul>
      </div>
    </div><!-- #collections -->
{% endif %}

Upvotes: 0

Views: 2256

Answers (1)

Steph Sharp
Steph Sharp

Reputation: 11682

There is a minor mistake on your capture line, which should be:

{% capture alt_attr %}{{ product.title }} by The Brave Collection{% endcapture %}

But other than that, your code worked fine for me. I pasted it into collection.liquid and changed the collection "brave-bracelets" and tag "cerulean" to a collection and tag of my own, and it displayed the list of products as expected. Maybe double check that you do have products with the tag "cerulean" in the collection "brave-bracelets"...

Upvotes: 1

Related Questions