How to Set Up Limited-Use Discount Codes

Control how many times customers can use discount codes by setting quantity limits that automatically validate codes and prevent overuse on your payment forms.

Build Time & Skill

15-20 min

Intermediate

What you'll learn

How to create discount codes with usage limits that automatically validate, display clear error messages, and apply discounts to order totals

How to Set Up Limited-Use Discount Codes

Discount codes drive sales and reward loyal customers, but unlimited codes create problems. Customers share codes beyond their intended audience, promotional budgets get exceeded, and you lose control over your campaigns. With Cognito Forms, you can set precise usage limits on discount codes that automatically validate codes, prevent overuse, and protect your profit margins.

This approach helps you:

  • Prevent discount abuse while rewarding legitimate customers. Set exact limits so codes can’t be shared endlessly or exploited beyond your intended promotion scope.
  • Create urgency with limited-time promotional codes. When customers know only 50 people can use a code, they act faster to claim their discount before it runs out.
  • Track exactly how many times each code has been used. See real-time usage data for every code without manual counting or spreadsheet tracking.
  • Automatically validate codes and show helpful error messages. Customers get instant feedback when they enter invalid codes or codes that have reached their limit.

Step-by-Step: Setting Up Limited-Use Discount Codes

Create a discount code field with quantity limits that validate codes and display error messages, then add a Price field that calculates and applies the discount to your order total. These five steps establish a complete discount code system that prevents overuse while rewarding customers.

Step 1: Add a discount code field

Start by adding a field where customers enter their discount code. A Textbox field works best because it allows customers to type any text, giving you the flexibility to create memorable codes.

  1. Add a Textbox field to your payment form
  2. Label it clearly, like “Discount Code” or “Promo Code”
  3. Leave the field optional so customers without codes can still complete their purchase

Start by adding a field where customers enter their discount code.


Step 2: Set quantity limits for each code

Configure how many times each discount code can be used by setting a quantity limit with a calculation that defines each code and its limit.

  1. Select your Discount Code field and find Limit Quantity in the field settings
  2. Choose Specific Quantity
  3. In the Quantity field, enter a calculation that defines each code and its usage limit:
    • For a single code: =if DiscountCode = "SAVE20" then 20 else null
    • For multiple codes: =if DiscountCode = "SAVE20" then 20 else if DiscountCode = "SAVE50" then 50 else if DiscountCode = "VIP10" then 10 else null
      This calculation checks the entered code against your valid codes. When someone enters “SAVE20,” the form allows 20 total uses. When the code doesn’t match any valid option, the calculation returns null, which means no limit exists – this triggers your “invalid code” error message in the next step.
  4. Include a Quantity Exceeded Error Message (ex: “This discount code has expired!”)

Set the Specific Quantity for the Discount Code field.

Quick Tip

Create meaningful, memorable codes. Use codes like “LAUNCH50” or “WELCOME10” instead of random characters. Customers remember these codes more easily and are more likely to use them.


Step 3: Add a custom error message

Configure an error message that appears when customers enter a code that doesn’t match any valid option.

  1. In your Discount Code field settings, select Show Custom Error - When
  2. Set the condition to: =(DiscountCode != null and DiscountCode_QuantityLimit = null)
  3. Enter a custom error message: “Invalid discount code. Please check your code and try again.”
    Display an error message when someone enters an invalid discount code.

This condition checks whether someone entered a code (DiscountCode != null) but that code has no quantity limit defined (DiscountCode_QuantityLimit = null), which means it’s not a valid code.


Step 4: Calculate and apply the discount

Add a Price field that calculates the discount amount and subtracts it from your order total.

  1. Add a Price field to your form and label it “Discount”

  2. In the Price field settings, find Amount and enter your discount calculation:

    • For percentage discounts: =if DiscountCode = "SAVE20" then -ItemTotal * 0.20 else if DiscountCode = "SAVE50" then -ItemTotal * 0.50 else 0
    • For fixed amount discounts: =if DiscountCode = "SAVE10" then -10 else if DiscountCode = "SAVE25" then -25 else 0
      Replace “ItemTotal” with the actual name of your payment field. If you have multiple payment fields, add them together: (PaymentField1 + PaymentField2 + PaymentField3).
      The negative values (-ItemTotal * 0.20 or -10) subtract from the total, creating the discount. When no valid code is entered, the calculation returns 0, applying no discount.
      In the Price field settings, find Amount and enter your discount calculation.
  3. Optionally, select Show This Field - When and set the Discount Price field to only appear when a discount applies: =(Discount > 0)

Set the Discount Price field to only appear when a discount applies

Important: When creating a discount calculation, always reference the actual payment fields on your form, not calculation properties like Order.SubTotal. The order subtotal property automatically recalculates after the discount is applied, creating a circular reference that yields incorrect discount amounts.


Step 5: Test your discount codes

Verify everything works correctly before customers see your form.

  • Click Preview in the form builder
    • Try entering an invalid code (like “TEST123”) and verify the “Invalid discount code” error appears
    • Enter a valid code and confirm the discount calculates correctly
  • Submit several test entries with the same code to approach the usage limit
  • Try using the code after reaching its limit and verify the “code has reached its usage limit” error appears

Test the Discount Code field with an invalid code.


Managing Multiple Discount Codes (Advanced Method)

When you have many discount codes or frequently add and remove promotional codes, managing them through calculations becomes tedious. Every code change requires editing the calculation and remembering the exact syntax. This advanced method uses a hidden Choice field to manage all your codes in one organized place, making updates faster and reducing errors.

Setup steps

Add a Choice field for the list of discount codes.

  1. Add a Choice field and label it “Available Discount Codes”
  2. Set the field Type to Dropdown
  3. Add a choice for each discount code you want to offer
  4. Select Limit Quantities and specify the usage limit for each code
  5. Select Assign Values and enter the discount percentage or amount for each code (0.20 for 20%, 0.50 for 50%)
  6. Set Default Value to =DiscountCode (this connects it to the Textbox field that customers see)
  7. Set Show This Field to Never

Now update your Discount Code Textbox field and Price field discount calculation:

  1. In your Discount Code field settings, change the Quantity calculation to: =AvailableDiscountCodes_Quantity
  2. Update the Show Custom Error condition to: =(DiscountCode != null and DiscountCode != AvailableDiscountCodes)
  3. In your Price field settings, change the Amount calculation to: =if DiscountCode = AvailableDiscountCodes then -ItemTotal * AvailableDiscountCodes_Value else 0

Enter a discount code to apply a discount to the order.

This approach references the hidden Choice field for all quantity limits and discount calculations. When you need to add a new code, just add it to the Choice field along with its quantity and value – no calculation editing required.


Additional Features to Enhance Your Discount Codes

Once your basic discount code system works smoothly, these advanced techniques create even more sophisticated promotions:

  • Apply discounts to repeating section totals for bulk order scenarios. Use the .Sum function to calculate discounts on the total of all items in a repeating section: =if DiscountCode = "BULK10" then -MyOrder.Sum(ItemTotal) * 0.10 else 0
  • Track code usage in Entry Views to monitor promotion performance in real-time. Create custom views filtered by discount code to see which promotions drive the most conversions.
  • Display remaining uses to create urgency. Add a Calculation field showing =DiscountCode_QuantityRemaining and conditionally display it when the code is valid. Customers see “Only 12 uses remaining!” and feel motivated to complete their purchase before the code runs out.

Start Creating Limited-Use Discount Codes Today

Limited-use discount codes transform uncontrolled promotions into strategic campaigns with measurable impact. By setting precise usage limits, you protect your profit margins while still rewarding customers with valuable discounts. Whether you’re running flash sales, building referral programs, or creating tiered promotions, quantity limits give you complete control over who uses your codes and how many times they’re redeemed.


FAQ