Why isn't my liquid code for cart conditions working properly?

Solved

Why isn't my liquid code for cart conditions working properly?

michaeluchiha
Shopify Partner
6 0 2

Hi, 

I have 2 items where I want to display a message in cart depending on the following scenarios:
1. User has a lite only in cart
You have a Lite in cart.
2. User has a Plus only in cart.
You have a Plus in cart
3. User has both in cart.
You have both lite and plus in cart.

The current script I wrote is
{%- for item in cart.items -%}
{% if item.product.type  == "Lite" and item.product.type != "Plus" %}
You have a Lite in cart.
{% elsif item.product.type  == "Plus" and item.product.type != "Lite" %}
You have a Plus in cart.
{% elsif item.product.type  == "Plus" and item.product.type == "Lite" %}
You have both lite and plus in cart.

{% endif %}
{% endfor %}

Can someone point me in the right direction please?

Accepted Solution (1)

Jason
Shopify Partner
11206 226 2313

This is an accepted solution.

You are looping over each item in the cart so this means you're looking at just one of them at a time.
A line like this will never work as i assume an item can not be both lite and plus.

 

{% elsif item.product.type  == "Plus" and item.product.type == "Lite" %}

 

If you want to keep that loop what's better is to set some variables first, and mark them as true or not as you asses the entire cart.

 

Try something like the below. Now this code could be slightly wrong as I've just typed it here quickly without actually testing. It should give you enough inspiration to get it working though.

  {%-  assign hasLite = false -%}
  {%-  assign hasPlus = false -%}

  {%- for item in cart.items -%}
    {%- if item.product.type  == "Lite" -%}
      {%-  assign hasLite = true -%}
      {%-  continue -%}
    {% elsif item.product.type  == "Plus" %}
      {%-  assign hasPlus = true -%}
      {%-  continue -%}
    {%  endif %}
  {% endfor %}

  {%- if hasLite and hasPlus -%}
    both
  {%-  elsif hasLite -%}
    just lite
  {%-  elsif hasPlus -%}
    just plus
  {%-  endif -%}
★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★

View solution in original post

Reply 1 (1)

Jason
Shopify Partner
11206 226 2313

This is an accepted solution.

You are looping over each item in the cart so this means you're looking at just one of them at a time.
A line like this will never work as i assume an item can not be both lite and plus.

 

{% elsif item.product.type  == "Plus" and item.product.type == "Lite" %}

 

If you want to keep that loop what's better is to set some variables first, and mark them as true or not as you asses the entire cart.

 

Try something like the below. Now this code could be slightly wrong as I've just typed it here quickly without actually testing. It should give you enough inspiration to get it working though.

  {%-  assign hasLite = false -%}
  {%-  assign hasPlus = false -%}

  {%- for item in cart.items -%}
    {%- if item.product.type  == "Lite" -%}
      {%-  assign hasLite = true -%}
      {%-  continue -%}
    {% elsif item.product.type  == "Plus" %}
      {%-  assign hasPlus = true -%}
      {%-  continue -%}
    {%  endif %}
  {% endfor %}

  {%- if hasLite and hasPlus -%}
    both
  {%-  elsif hasLite -%}
    just lite
  {%-  elsif hasPlus -%}
    just plus
  {%-  endif -%}
★ I jump on these forums in my free time to help and share some insights. Not looking to be hired, and not looking for work. http://freakdesign.com.au ★