Require minimum quantity for a specific item to checkout

Daniel_Pape
Tourist
5 0 4

I need to require a minimum item quantity for a specific SKU in my store before allowing checkout to continue.

I have seen a few topics that seem to flirt with a solution, but none seem to quite get there.

I am not interested in paying $4.99 for an APP that provides a workaround.

I am toying with something like this:

{% for item in cart.items %}
   {% if variant.sku== ‘MYSKU’ %}
      {% if forloop.first %}
         {% if cart.items.size >= 24 %}
            You must purchase a minimum of 24 MYITEMTITLE.<p>
            {% else %}
               <input type="submit" value="Checkout Now" name="checkout" class="primary" />
            {% endif %}
      {% endif %}
   {% endif %}
{% endfor %}

Thanks very much.

Daniel_Pape
Tourist
5 0 4

Hey guys,

For those who will come after. I got this figured out.

This code will see if the cart contains any of the items requiring a minimum quantity, adds up the total of the required only items, and then checks to see if it matches the required number for order. This check is self contained an ignores item quantity that do not require a minimum for checkout. My items had multiple variants so line 3 checks for the parent SKU string rather than the variant.sku.

{% assign cartcount = 0 %}        
{% for item in cart.items %}
        {% if item.sku contains 'ST714' or item.sku contains 'ST717' %}        
        	{% capture cartcount %}{{ cartcount | plus: item.quantity }}{% endcapture %}        	
        {% endif %}
{% endfor %}
{% if cartcount >= '24' %}
        <input type="submit" id="checkout" class="btn" name="checkout" value="Checkout" />
{% else %}
        <div class="minalert">You must order at least 24 Dealer T-Shirts at a time. Please update your quantities or <a href="/products/dealer-tshirt-program">add more Dealer T-Shirts</a></div>
        <div class="clearfix"></div>
{% endif %} 

The following cart.liquid lines were edited.

<div class="buttons clearfix">
      <input type="submit" id="checkout" class="btn" name="checkout" value="Checkout" />
      <input type="submit" id="update-cart" class="btn" name="update" value="Update" />
</div>

becomes

<div class="buttons clearfix">

{% assign cartcount = 0 %}        
{% for item in cart.items %}
        {% if item.sku contains 'ST714' or item.sku contains 'ST717' %}        
        	{% capture cartcount %}{{ cartcount | plus: item.quantity }}{% endcapture %}        	
        {% endif %}
{% endfor %}
{% if cartcount >= '24' %}
        <input type="submit" id="checkout" class="btn" name="checkout" value="Checkout" />
{% else %}
        <div class="minalert">You must order at least 24 Dealer T-Shirts at a time. Please update your quantities or <a href="/products/dealer-tshirt-program">add more Dealer T-Shirts</a></div>
        <div class="clearfix"></div>
{% endif %}          
          
      <input type="submit" id="update-cart" class="btn" name="update" value="Update" />
</div>

Hope this helps someone.

0 Likes
Daniel_Pape
Tourist
5 0 4

Update.

I was running into an issue where carts that did not contain minimum required product items where still hiding the checkout button. It appeared it was still attempting to do the cartcount math even though non of the required items were in the cart. This was generating a math error and hiding the checkout button.

This code sets a variable to test if the minimum check should take place. If so, performs as posted above, if not, it ignores the math check and prints a checkout button as originally intended.

<!-- start custom cart code -->
<!-- custom check for minimal items and gets a count of those items -->
{% assign cartcount = 0 %}        
{% for item in cart.items %}
        {% if item.sku contains 'ST714' or item.sku contains 'ST717' %}        
            {% assign buttoncheck = 'yes' %}
            {% capture cartcount %}{{ cartcount | plus: item.quantity }}{% endcapture %}            
        {% endif %}       
{% endfor %}
{% assign cartcount = cartcount | times: 1 %}
<!-- if minimal items are in the cart, hide the checkout button unless they are ordering the proper amount -->
{% if buttoncheck == 'yes' and cartcount < 24 %}          
            <div style="float:right;font-weight:bold;text-align:right;width:400px;margin-bottom:20px;">You must order at least 24 Dealer T-Shirts at a time. Please update your quantities or <a href="/products/dealer-tshirt-program">add more Dealer T-Shirts</a></div>
            <div class="clearfix"></div>  
{% endif %}
{% if buttoncheck == 'yes' and cartcount >= 24 %}   
            <input type="submit" id="checkout" class="btn" name="checkout" value="Checkout" /> 
{% endif %}
{% if buttoncheck != 'yes' %} 
      <input type="submit" id="checkout" class="btn" name="checkout" value="Checkout" /> 
{% endif %}
<!-- end custom cart code -->

 

0 Likes
morganchase
New Member
1 0 0

This is many years later but I've just come across your Q&A here while searching for a different solution. My business has several main products and some accompanying products...at this time I do not want the accompanying products sold by themselves so I want to require a specific 'tag' or 'collection' before checkout is allowed. Can this be adapted to that?

0 Likes