How do i check products in cart for tags?

Solved
Highlighted
Tourist
8 1 1

Hi there!

 

I'm wondering how I can check cart items for specific tags to display something else.

 

{% for item in cart.items %}
{% if item.product.tags contains 'poster' and item.product.tags contains 'frames' %}
   hello
{% endif %}
{% endfor %}

My problem is that it is currently checking for "poster" and "frames" for one product but I want them to check if any item contains these tags.

 

If I change the code to ... 

 

{% for item in cart.items %}
{% if item.product.tags contains 'poster' or item.product.tags contains 'frames' %}
   hello
{% endif %}
{% endfor %}

I get two greetings. Two hello's.

 

Any ideas?

 

0 Likes
Highlighted
Shopify Partner
1025 215 468

@remi11 

 

Use following code:

{% assign isTagAvailable = false %}
{% for item in cart.items %}
{% if item.product.tags contains 'poster' or item.product.tags contains 'frames' %}
   {% assign isTagAvailable = true %}
{% endif %}
{% endfor %}

{% if isTagAvailable  %}
     Add your code here
{% endif %}

Cheers,

Tejas

Shopify Expert | Skype: tejas.nadpara
- Like and Mark as an Accepted Solution if reply helpful
- Feel free to contact me on support@hexaecommerce.com regarding any help
1 Like
Highlighted
Tourist
8 1 1

Hello @Tejas_Nadpara, thank you for replying. 

 

Apparently the code of yours did not do the trick. It's still displaying the the code regardless. I'm now previewing with only one product in cart when this should be impossible to see. Maybe I have not explained well, so I'll make it into a scenario.

 

In my current cart there is only one item, this item contains "poster" tag. When I view the cart I do not see the code. I remove this product from the cart now. I am then adding a product that contains the "frames" tag. I am still not able to see the code when I view the cart. I then add a product with poster tag and I when I am to view the cart I see the code.


Only product with "frames" tag = hidden

Only product with "poster" tag = hidden

Products with "poster" and "frame" tag = show

 

 

0 Likes
Highlighted
Shopify Partner
1025 215 468

@remi11 

 

Send me both product url so, I can take a look and assist you better.

 

Thank you,

Tejas

Shopify Expert | Skype: tejas.nadpara
- Like and Mark as an Accepted Solution if reply helpful
- Feel free to contact me on support@hexaecommerce.com regarding any help
0 Likes
Highlighted
Tourist
8 1 1

Hi @Tejas_Nadpara,

 

Sent you a PM including link and password for my platform.

 

Thank you.

0 Likes
Highlighted
Shopify Partner
1025 215 468

This is an accepted solution.

@remi11 

 

Try this code, Your custom code only include if both tags are available in cart :

{% assign isPosterAvailable = false %}
{% assign isFramesAvailable = false %}
{% for item in cart.items %} {% if item.product.tags contains 'poster' %} {% assign isPosterAvailable = true %} {% endif %} {% if item.product.tags contains 'frames' %} {% assign isFramesAvailable = true %} {% endif %} {% endfor %} {% if isPosterAvailable and isFramesAvailable %} Add your code here {% endif %}

Let me know if you need any other help! :)

 

Thank you,

Tejas

Shopify Expert | Skype: tejas.nadpara
- Like and Mark as an Accepted Solution if reply helpful
- Feel free to contact me on support@hexaecommerce.com regarding any help
1 Like
Highlighted
Tourist
8 1 1

@Tejas_Nadpara 

 

SWEET! This is working just the way I wanted, thank you, thank you!

1 Like
Highlighted
Excursionist
14 1 6

Note that product tags are available only in the cart template. The GET /cart call has no support for items[].product_tags. Instead, one must grab items[].handle (not items[].product_id or items[].product_handle as one might assume with standard object selection methods or consistent naming conventions) and pass it to the GET /products/{product-handle} call to get tags[].

 

Time to see if I can keep multiple parallel callbacks in order while iterating the cart and triggering a series of /cart/updates. This should also make for an amusing customer experience under certain circumstances. My apologies in advance to anyone who might be epileptic as I have potentially lost the ability to make a single refresh.

0 Likes