Conditional message based on items tag present in cart for shopify

Solved

Conditional message based on items tag present in cart for shopify

Marcwales
New Member
4 0 0

Hi, 

I've managed to code something basic to obtain the following :

- I sell items that have different sizes (size A, B, C, D, E).

- Some customers make the mistake of adding different sizes in the cart.

- So i want to show a message saying "Warning, you have added 2 different sizes to your cart"

 

Here is the code i've used,  it works but it's not perfect and i'm open to suggestions. I was looking for something like "does not contain" but i can't seem to understand correctly the "unless" function : 

 

{% assign tagA = 0 %}
{% assign tagB = 0 %}
{% assign tagC = 0 %}
{% assign tagD = 0 %}
{% assign tagE = 0 %}

{% for item in cart.items %}
       {% if item.product.tags contains 'A' %}
              {% assign tagA = 1 %}
       {% endif %}
       {% if item.product.tags contains 'B' %}
              {% assign tagB = 1 %}
       {% endif %}
       {% if item.product.tags contains 'C' %}
              {% assign tagC = 1 %}
       {% endif %}
       {% if item.product.tags contains 'D' %}
              {% assign tagD = 1 %}
       {% endif %}
       {% if item.product.tags contains 'E' %}
              {% assign tagE = 1 %}
       {% endif %}
{% endfor %}

{% if tagA > 0 and tagB > 0 or tagC > 0 or tagD > 0 or tagE > 0 %}
       Your cart has 2 different size (Size A and another), please check if accurate.
{% endif %}

 

Hope someone can help me. 

Regards, 

Accepted Solution (1)

namphan
Shopify Partner
1134 142 175

This is an accepted solution.

Hi @Marcwales,

Please change all code:

{%- liquid
  assign count_tag = 0
  for item in cart.items
    if item.product.tags contains 'A' or item.product.tags contains 'B' or item.product.tags contains 'C' or item.product.tags contains 'D' or item.product.tags contains 'E'
     assign count_tag = count_tag | plus: 1
    endif
  endfor
-%}

{%- if count_tag > 1 -%}
  Your cart has 2 different size (Size A and another), please check if accurate.
{%- endif -%}
Coffee tips fuels my dedication.
Shopify Development Service
Need help with your store? namphan992@gmail.com

View solution in original post

Replies 7 (7)

namphan
Shopify Partner
1134 142 175

This is an accepted solution.

Hi @Marcwales,

Please change all code:

{%- liquid
  assign count_tag = 0
  for item in cart.items
    if item.product.tags contains 'A' or item.product.tags contains 'B' or item.product.tags contains 'C' or item.product.tags contains 'D' or item.product.tags contains 'E'
     assign count_tag = count_tag | plus: 1
    endif
  endfor
-%}

{%- if count_tag > 1 -%}
  Your cart has 2 different size (Size A and another), please check if accurate.
{%- endif -%}
Coffee tips fuels my dedication.
Shopify Development Service
Need help with your store? namphan992@gmail.com
Marcwales
New Member
4 0 0

Hi Namphan, 

 

That is perfect, works like a charm. Is there any way to actually know what sizes the customer has put in his cart so that i can say : 

- "Your cart has 2 different size (Size ? and size ?)

 

Could i use : 

{%- if count_tag_a and count_tag_b > 1 -%}

Your cart has 2 different size (Size A and Size B), please check if accurate.

{%- endif -%}

{%- if count_tag_a and count_tag_c > 1 -%}

Your cart has 2 different size (Size A and Size C), please check if accurate.

{%- endif -%}

 

This will again complicate your code which wasn't the goal at first 😉 

 

Thank you again for your help.

 

Regards

namphan
Shopify Partner
1134 142 175

Hi @Marcwales,

Please change code:

 

{%- liquid
  assign count_tag = 0
  assign name_tag = ''
  for item in cart.items
    if item.product.tags contains 'A' or item.product.tags contains 'B' or item.product.tags contains 'C' or item.product.tags contains 'D' or item.product.tags contains 'E'
     assign count_tag = count_tag | plus: 1
     assign name_tag = name_tag | append: item.product.tags | append: ';'
    endif
  endfor
  assign size_tag = name_tag | split: ';' %}
-%}

{%- if count_tag > 1 -%}
  Your cart has {{ size_tag.size }} different size ({% for name in size_tag %}Size {{ name }} {% unless forloop.last %}and{% endunless %}{% endfor %}), please check if accurate.
{%- endif -%}

 

Coffee tips fuels my dedication.
Shopify Development Service
Need help with your store? namphan992@gmail.com
Marcwales
New Member
4 0 0

Hi Namphan, 

 

Thank you for your fast response. That code looks awesome and did the trick, but it shows all the tags associated to that product. Not just the size tag. Is this something achievable or should i just use your first code which is already great ? 

 

Regards, 

namphan
Shopify Partner
1134 142 175

Hi @Marcwales,

Cann I send you a collaborator invite? it will help me review the code

Coffee tips fuels my dedication.
Shopify Development Service
Need help with your store? namphan992@gmail.com
Marcwales
New Member
4 0 0

Hi Namphan, 

 

Sorry for the delay, i was on vacation. I'm waiting for the theme vogal to work on the website and then i'll send you a collaborator invite for you to see the code. That is very kind of you. 

 

Again big thanks for your help. 


Regards,

Sidney

namphan
Shopify Partner
1134 142 175

Hi @Marcwales,

Sure, you can also send me an email at signature, I will check everything faster

Coffee tips fuels my dedication.
Shopify Development Service
Need help with your store? namphan992@gmail.com