Preventing checkout unless certain combinations of items are in the cart

Tourist
6 0 1

Hi Everyone,

I'm working on a shop that's selling devices that require a subscription plan, much like a mobile phone company. 

I'm trying to add logic to the cart before the user checks out that will block checkout if the device/plan combination isn't compatible, or if they try to get a device with out a subscription or vice versa. 

My current attempt was to add:

 {% assign device_in_cart = false %}
{% for item in cart.items %}
  {% if item.product.tags contains 'device' %}
      {% assign assure_in_cart = false %}
   {% else %}       
    {% assign assure_in_cart = true %}
  {% endif %}
{% endfor %}

{% if assure_in_cart %}
          <div class="grid-item large--one-whole">
          <h1 style="background-color: red; color: white;">YOU MUST CHOOSE A DEVICE AND SUBSCRIPTION BEFORE CHECKING OUT</h1>
          </div>

----------

but that throws the flag if there's any item in the cart that doesn't follow the rules. 

is there a way to make this work? I can't seem to find anything like this any where else.

thanks for your help!

0 Likes
Tourist
6 0 1

second attempt is getting strange results:

 {% assign step_two = false %}
          {% assign discount_step_one = false %}
     {% for item in cart.items %}
          
          {% if item.product.id == '5870914567' or '5870914439' %}
          {% assign discount_step_one = true %}
          {{ item.product.title }}
          {% else %}
          {% assign discount_step_one = false %}
          {% endif %}
          {{ discount_step_one }}
          {{}}
     {% endfor %}
     {% for item in cart.items %}
          {% if item.product.handle == 'callsafe-assure-semi-annual-subscription-1' or 'callsafe-assure-yearly-subscription-1' or 'callsafe-go-semi-annual-subscription-1' or 'callsafe-go-yearly-subscription-1' %}
              {{ item.product.title }}
              {% assign step_two = true %}
          {% else %}
              {% assign step_two = false %}
          {% endif %}
          {{ step_two }}
     {% endfor %}
          {% if discount_step_one %}
              {% if step_two %}
          <div class="grid-item large--one-whole"><h2>SUCCESS</h2></div>
          {% else %}
          <div class="grid-item large--one-whole"><h2>FAILED</h2></div>
          {% endif %}
          {% endif %}

 

if one varible returns true, they both do and i can't figure out why.

0 Likes
Tourist
6 0 1

For anyone else that's trying to implement something simular, I finally got the logic to work. Here's the code: 

          <!-- TEST TO MAKE SURE DEVICE AND SUBSCRIPTION MATCH UP -->
           {% assign device_go = false %}
       	   {% assign device_assure = false %}
           {% assign sub_go = false %}
       	   {% assign sub_assure = false %}
           {% assign all_clear_go = false %}
           {% assign all_clear_assure = false %}
     {% for item in cart.items %}
          {% if item.product.handle == 'callsafe-go' %}
          {% if item.product.handle != 'subscription' %}
          {% assign device_go = true %}
          {% else %}
          {% endif %}{% endif %}
          {% if item.product.handle == 'callsafe-assure' %}
          {% if item.product.handle != 'subscription' %}
          {% assign device_assure = true %}
          {% else %}
          {% endif %}{% endif %}
     {% endfor %}
       
     {% for item in cart.items %}
          {% if item.product.handle contains 'callsafe-go' %}
          	{% if item.product.handle contains 'subscription'%}
          	{% assign sub_go = true %}
          	{% endif %}
          	{% endif %}
          {% if item.product.handle contains 'callsafe-assure' %}
          	{% if item.product.handle contains 'subscription'%}
          		{% assign sub_assure = true %}
          	{% endif %}
          {% endif %}
     {% endfor %}
          {% if device_go == true %}
          	{% if sub_go == true %}
          		{% assign all_clear_go = true %}
			{% else %}	
          {% endif %}
          {% endif %}
		  {% if device_assure == true %}
          	{% if sub_assure == true %}
          		{% assign all_clear_assure = true %}
			{% else %}	
          {% endif %}
          {% endif %}
        
          {% if device_go %}
          {%if all_clear_go == true %}
          		<div class="grid-item large--one-whole"><h3>Congratulations! You're all set!</h3></div>
          			
          	{% else %}
          <div class="grid-item large--one-whole"><h3>Please choose a <a href="/pages/catalog-page-1">device and subscription!</a></h3></div>
          	{% endif %}{% endif %}
          {%if device_assure %}
          {%if all_clear_assure == true %}
          		<div class="grid-item large--one-whole"><h3>Congratulations! You're all set!</h3></div>
          			
          	{% else %}
          		<div class="grid-item large--one-whole"><h3>Please choose <a href="/pages/catalog-page-1">device and subscription!</a></h3></div>
          	{% endif %}{% endif %}
          <!-- END DEVICE/SUB TEST -->

then wrapped the Terms and Checkout button in another if condition to check if all_clear was true.

1 Like
Highlighted
Shopify Partner
104 0 11

Thanks, this is useful for something completely different that I am working on.

Shopify expert, software developer and designer.
0 Likes