Is there anyway i can use the 'where' filter on collection.all.products to filter by tags?

Solved

Is there anyway i can use the 'where' filter on collection.all.products to filter by tags?

Timed_Ecom
Shopify Partner
39 1 13

I need to access products in the collection.all.products array by a certain tag on a normal page template. 
I can't put this into their own seperate collection they need to be accessed in the collection.all collection. 

Is there any way i can do this via liquid??
I can use the where filter to return products with other attributes like the below code will return any product with the vendor 'Apple'.

{% assign current_products = collections.all.products | where: 'vendor', 'Apple' %}
{% for product in current_products %}
{{ product.vendor }}
{% endfor %}

But is there any way I can use some kind of filter to return products that have a tag that is in the collection.all collection??

Something like this code below? But obviously, it does not work?

{% assign current_products = collections.all.products | where: 'tags', 'phone' %}
{% for product in current_products %}
{{ product.title }}
{% endfor %}

 

Thanks 

Accepted Solution (1)

PaulNewton
Shopify Partner
7722 678 1626

This is an accepted solution.

No.

The where filter operates on the passed array based on a property of the individual items in that aray.

Not on objects, or an objects arrays

A resources tags have some convenience behaviors when working with them but they aren't a single property which of course leads to hopeful expectations they should be useful for something like the |where: filter.

 

Currently this is solved with either:

A specific collection setup with conditions based on the target tag.

Or if you can do this on the client side just fetch that tag filter collection url using a special template you use specifically for ajax or the section rendering api. Keeping in mind pagination.

/collections/collectionname/tagname

 

What you don't want to do is try to loop over all products in an ALL collection.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


View solution in original post

Replies 2 (2)

PaulNewton
Shopify Partner
7722 678 1626

This is an accepted solution.

No.

The where filter operates on the passed array based on a property of the individual items in that aray.

Not on objects, or an objects arrays

A resources tags have some convenience behaviors when working with them but they aren't a single property which of course leads to hopeful expectations they should be useful for something like the |where: filter.

 

Currently this is solved with either:

A specific collection setup with conditions based on the target tag.

Or if you can do this on the client side just fetch that tag filter collection url using a special template you use specifically for ajax or the section rendering api. Keeping in mind pagination.

/collections/collectionname/tagname

 

What you don't want to do is try to loop over all products in an ALL collection.

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


Timed_Ecom
Shopify Partner
39 1 13

Thanks @PaulNewton. Make perfect sense. Will test a few of the suggestions you noted. 

Cheers!