Operators for "If X but does not include Y"?

Solved
cotty
Excursionist
22 0 7

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?

Accepted Solution (1)
Kani
Shopify Partner
463 122 202

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 %}

 

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂

View solution in original post

Replies 2 (2)
Kani
Shopify Partner
463 122 202

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 %}

 

Hey!! ʕ ᵔᴥᵔ ʔ
Please click 'Like' and ' Accept as Solution' to encourage me to continue sharing my expertise. ♡
If you need any technical assistance, feel free to send me a DM. You no longer have to search for answers without getting a response. 🙂
cotty
Excursionist
22 0 7

@Kanithank you so much. You've saved me from pulling out all of my hair. 😢😊 Really appreciate it!