If and elsif else statements for multiple conditions

New Member
10 0 0

I am looking to add a bit of code to my checkout page that is gives
AB if A and B are purchased
or gives A if A (without B) is purchased
or gives B if B (without A) is purchased
else returns nothing.

So far I've got this:

{% for line in checkout.line_items %}
  {% if line.title contains "Hello" and "World" %}
  1 Hello and World 1 </br>
 {% elsif line.title contains "Hello" %}
  2 Hello 2 </br>
 {% elsif line.title contains "World" %}
  3 World 3 </br>
 {% else %}
  4 None. 4</br>
 {% endif %}
{% endfor %}

That's returning: [edited to say that: I have both Hello and World products in my cart. And if it matters, World product appears first alphabetically.]

3 World 3 
1 Hello and World 1 

I can't figure out why it's returning 3 before 1 or why it's returning 3 at all or why if 3, but not 2. So confuseing. 

Any help would be greatly appreciated. 

 

0 Likes
Shopify Expert
9807 92 1560

Hey Adam,

Try changing this line:

{% if line.title contains "Hello" and "World" %}

into this:

{% if line.title contains "Hello" and line.title contains "World" %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
New Member
10 0 0

Jason, that gives the same, no change.

0 Likes
New Member
10 0 0

Okay, another interesting tidbit. If I switch the order of the elsif lines it changes the output.

Original code:

{% elsif line.title contains "Hello" %}
  2 Hello 2 </br>
{% elsif line.title contains "World" %}
  3 World 3 </br>

Original gives: 

3 World 3 
1 Hello and World 1 

Switched order code:

{% elsif line.title contains "World" %}
  3 World 3 </br>
{% elsif line.title contains "Hello" %}
  2 Hello 2 </br>

2 Hello 2 
1 Hello and World 1 

Still no clue why it's evaluating the second elsif then the if though.

0 Likes
New Member
10 0 0

Also, played with below, but it gives the same output as above:

{% for line in checkout.line_items %}
{% assign condition1 = false %}
{% assign condition2 = false %}
{% assign condition3 = false %}
{% if line.title contains "A" and "B" %}
{% assign condition1 = true %}
{% endif %}
{% if line.title contains "A" %}
{% assign condition2 = true %}
{% endif %}
{% if line.title contains "B" %}
{% assign condition3 = true %}
{% endif %}
{% if condition1 %}
Condition 1</br>
{% elsif condition1 == false and condition2 == true and condition3 == false %}
Condition 2 </br>
{% elsif condition1 == false and condition2 == false and condition3 == true %}
Condition 3 </br>
{% else %}
None.</b>
{% endif %}
{% endfor %}

When I have both A and B products checked out the checkout page shows:

Condition 3 
Condition 1

And if I switch the elsif statements the output changes to 

Condition 2 
Condition 1

 

0 Likes
Shopify Expert
9807 92 1560

Perhaps you're not approaching the logic correctly.

This line will check to see if the string hello and world both appear in single line.title and has nothing to with checking multiple products.

{% if line.title contains "Hello" and "World" %}

Getting back to this comment:

AB if A and B are purchased
or gives A if A (without B) is purchased
or gives B if B (without A) is purchased

You need to loop over the entire cart first. It looks like you've realised this in the last post by setting the condition vars. One immediate issue I see is that you're setting those condition vars within the loop so they are getting reset each time - that can't be right.

I'll just ignore your code for now and throw this into the mix.

{% assign lineItemTitles = checkout.line_items | map:'title' %}
{% if lineItemTitles contains "A" and lineItemTitles contains "B" %}
  contains items with title of A and title of B
{% elsif lineItemTitles contains "A" %}
  contains title of A
{% elsif lineItemTitles contains "B" %}
  contains title of B
{% endif %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
0 Likes
New Member
10 0 0

That doesn't seem to be working. I get nothing. Seems like there's something wrong with even this smaller bit, still get nothing returning here:

{% assign lineItemTitles = checkout.line_items | map:'title' %}
{% if lineItemTitles contains "A" %}
  contains items with title of A 
{% endif %}

 

0 Likes
Shopify Expert
9807 92 1560

I'd have to assume that there is not a line item with the exact title of 'A' (or whatever you're actually using here) in the cart. The map filter takes all the titles and adds them into an array. When you do a contains again the array the value must be an exact match and not a partial.

A real life tested example that works in my checkout.liquid file would be this:

{% assign lineItemTitles = checkout.line_items | map:'title' %}
{% if lineItemTitles contains "5 Pocket Jean - 30 / Indigo" %}
  Yep, there's a '5 Pocket Jean - 30 / Indigo' in the cart
{% endif %}

Without seeing the code in use, or knowing the titles of the line items it is a little hard to guide you here. We're largely guessing without the larger scope.

If you need to do partial matching of the title you need to loop over the line items. So to keep my example product and search for the partial of "5 Pocket Jean":

{% assign hasJeans = false %}
{% for line in checkout.line_items %}
  {% if line.title contains "5 Pocket Jean" %}
    {% assign hasJeans = true %}
  {% endif %}
{% endfor %}

{% if hasJeans %}
  I found jeans
{% endif %}

^ this works in my checkout as well.

To debug, consider outputting things like the line item title so you know what values you're really dealing with.

For completeness let's rewrite your previous code so the logic is fixed (and moving the variable assignment outside of the loop)

{% assign hasJeans = false %}
{% assign hasPants = false %}

{% for line in checkout.line_items %}
  {% if line.title contains "5 Pocket Jean" %}
    {% assign hasJeans = true %}
  {% elsif line.title contains "5 Pocket Pants" %}
    {% assign hasPants = true %}
  {% endif %}
{% endfor %}

{% if hasJeans and hasPants %}
  Has both jeans and pants
{% elsif hasJeans %}
  Has only jeans
{% elsif hasPants %}
  Has only pants
{% endif %}

 

★ Winning Partner of the Build a Business competition. ★ http://freakdesign.com.au
1 Like
New Member
10 0 0

Thank you Jason!! That worked. It was a partial match problem.

Yippie!!

One more question (if I'm not pushing my luck):

Previously I was able to copy and paste the code that worked on the checkout page into the confirmation email under the line.item section, but that doesn't seem to be working any longer. Even with code that previously worked or your code above that's working. It doesn't show up on the confirmation email. Any ideas?

0 Likes
New Member
10 0 0

Apparently there was a bug with the email forms. The above code now works in the confirmation emails. Thanks Shopify!

0 Likes