Shopify themes, liquid, logos, and UX
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,
Solved! Go to the solution
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 -%}
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 -%}
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
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 -%}
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,
Hi @Marcwales,
Cann I send you a collaborator invite? it will help me review the code
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
Hi @Marcwales,
Sure, you can also send me an email at signature, I will check everything faster
By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024Thanks to everyone who participated in our AMA with 2H Media: Marketing Your Shopify St...
By Jacqui Sep 6, 2024