Assign Product Quantity Limit to Items in Collection

General

This technique was first used on the RNT Calls website. They had a large number of products and assigning each product to a unique template would have been time consuming. This way they can navigate to the collection they want and assign products quickly.

When to use

Use this technique when the client requires a large number of products to have limited sales per transaction.

How the code works

The code runs a collection loop and checks if the current product is within any of the quantity-limit collections. If the product is within one of those collections, then assign a limit to the productmaxqty variable. This will disable the checkout button and alert the user to change their quantity.

Requirements: This technique is for newer OLM themes. If the theme is missing the product quantity limit templates, then you will needd to copy that functionality from a newer theme before applying this code.

Create Quantity Limit Collections

Create Quantity Limit collections and assign the appropriate products.

NOTE: The code relies on the naming of the collection handle, so make sure the handles use one of the following naming conventions:

quantity-test-1
quantity-test-2
quantity-test-3
quantity-test-4
quantity-test-5

The Code

product-add-to-cart-form.liquid

Open the product-add-to-cart-form.liquid. Find the section that checks if product.template_suffix == 'quantity-limit-1' and add the following code before that section.
  {% comment %}
    ===================================================================================
      START COLLECTION QUANTITY LIMIT
      When a product is within a collection with a quantity limit, then limit 
      the number of purchases.
    ===================================================================================
    ===================================================================================
  {% endcomment %}

  {% assign in_quantity_limit_collection = false %}

  {% for collection in product.collections %}
    {% if in_quantity_limit_collection == false and collection.handle == 'quantity-limit-1' %}
      {% assign quantity_limit_collection = true %}
      {% assign quantity_limit_number = 1 %}
    {% elsif in_quantity_limit_collection == false and collection.handle == 'quantity-limit-2' %}
      {% assign quantity_limit_collection = true %}
      {% assign quantity_limit_number = 2 %}         
    {% elsif in_quantity_limit_collection == false and collection.handle == 'quantity-limit-3' %}
      {% assign quantity_limit_collection = true %}
      {% assign quantity_limit_number = 3 %}   
    {% elsif in_quantity_limit_collection == false and collection.handle == 'quantity-limit-4' %}
      {% assign quantity_limit_collection = true %}
      {% assign quantity_limit_number = 4 %}            
    {% elsif in_quantity_limit_collection == false and collection.handle == 'quantity-limit-5' %}
      {% assign quantity_limit_collection = true %}
      {% assign quantity_limit_number = 5 %}         
    {% endif %}         
  {% endfor %}

  {% if quantity_limit_collection %}
      {% assign product_max_qty = quantity_limit_number %}
      {% for line_item in cart.items %}
        {% if product.id == line_item.product_id %}
          {% assign disable_btn = true %}
        {% endif %}
      {% endfor %}
  {% endif %}

cart.liquid

Open cart.liquid and find {% assign productmaxqty = blank %}. Add the following code directly after:
{% for collection in i.product.collections %}
  {% if collection.handle == 'quantity-limit-1' %}
    {% assign product_max_qty = 1 %}
  {% elsif collection.handle == 'quantity-limit-2' %}
    {% assign product_max_qty = 2 %}
  {% elsif collection.handle == 'quantity-limit-3' %}
    {% assign product_max_qty = 3 %}             
  {% elsif collection.handle == 'quantity-limit-4' %}
    {% assign product_max_qty = 4 %}             
  {% elsif collection.handle == 'quantity-limit-5' %}
    {% assign product_max_qty = 5 %}                                        
  {% endif %}                             
{% endfor %}

Leave a comment