Liquid, JavaScript, themes, sales channels
Hi, I've been through the forums and found similar discussions, but nothing quite the same, so hopefully someone could help me with the below.
Trying to show different messages depending on what is in cart.
Message 1:
If at least 1 item in cart exists with 'Bundle' and NOT 'Profile' show "MSG 1"
If product tags found in cart fo 'Bundle' and 'Profile' then show: "MSG 2"
I used the below, but unfortunately, the output was showing message; "MSG 2" twice due to 'Bundle' tag visible in both:
{% for item in cart.items %}
{% if product.tags == Bundle and Profile %}
MSG 1
{% endif %}
{%if product.tags == Bundle %}
MSG 2
{% endif %}
{% endfor %}
So I tried the below to ensure no duplicate message with 'Bundle' & != 'Profile' found in cart, i.e., was expecting just "MSG 2" to show once, but instead, shows nothing:
{% for item in cart.items %} {% if product.tags == Bundle and Profile %} MSG 1 {% endif %} {%if product.tags == Bundle and product.tags != Profile %} MSG 2 {% endif %} {% endfor %}
Am I a million miles away from what I'm trying to do here? I guess in the second example, I'm not allowed to add 'if this, but not that', but I'm not sure how to get around that.
Is anyone able to point me in the right direction, please?
Solved! Go to the solution
This is an accepted solution.
Hi @cotty
You can try to do it like this
// put this code at top
{%- assign bundle_count = 0 -%}
{%- assign profile_count = 0 -%}
{% for item in cart.items %}
{% for tag in item.product.tags %}
{% if tag == 'Bundle' %}
{%- assign bundle_count = bundle_count | plus: 1 -%}
{% break %}
{% endif %}
{% if tag == 'Profile' %}
{%- assign profile_count = profile_count | plus: 1 -%}
{% break %}
{% endif %}
{% endfor %}
{% endfor %}
// render your msg in cart item
{% for item in cart.items %}
{% if bundle_count > 0 and profile_count > 0 %}
MSG 1
{% endif %}
{%if bundle_count > 0 and profile_count == 0 %}
MSG 2
{% endif %}
{% endfor %}
This is an accepted solution.
Hi @cotty
You can try to do it like this
// put this code at top
{%- assign bundle_count = 0 -%}
{%- assign profile_count = 0 -%}
{% for item in cart.items %}
{% for tag in item.product.tags %}
{% if tag == 'Bundle' %}
{%- assign bundle_count = bundle_count | plus: 1 -%}
{% break %}
{% endif %}
{% if tag == 'Profile' %}
{%- assign profile_count = profile_count | plus: 1 -%}
{% break %}
{% endif %}
{% endfor %}
{% endfor %}
// render your msg in cart item
{% for item in cart.items %}
{% if bundle_count > 0 and profile_count > 0 %}
MSG 1
{% endif %}
{%if bundle_count > 0 and profile_count == 0 %}
MSG 2
{% endif %}
{% endfor %}
@Kanithank you so much. You've saved me from pulling out all of my hair. 😢😊 Really appreciate it!
User | RANK |
---|---|
25 | |
22 | |
9 | |
6 | |
5 |
Explore the 30-30-30 rule, a dynamic social media strategy for new businesses. Learn how t...
By Trevor Sep 20, 2023Discover how to leverage the often overlooked footer of your ecommerce site to gain custom...
By Skye Sep 15, 2023In this blog, we’ll be shining a light on Shopify Partners, Experts, and Affiliates. Who a...
By Imogen Sep 13, 2023