Discount codes example

This is a very simple example of how you can implement discount codes managed in a separate Excel file on the server.

Try these codes:

TESTPERCENT – percentage discount, 20%
TESTFIXED – fixed discount of $5

You can download the Excel spreadsheet here.

    Source code for this form:

    <label>Subtotal ($):
    $ [text subtotal "1000"]</label>
    <label>Discount code:
    [text discount-code]</label>
    <label>Second discount code:
    [text second-discount-code]</label>
    [calculate_button calculate_button-1 "Calculate"]
    <div style="display:none">
    <!-- Look up the discount code and match it to a number -->
    [spreadsheet_reference raw_discount cf7-hide field:discount-code sheet:1 cells:A1-B10 ""]
    [spreadsheet_reference raw_discount_2 cf7-hide field:second-discount-code sheet:1 cells:A1-B10 ""]
    <!-- We need to apply some behind-the-scenes number transformations to see if the discount is a fixed amount or a percentage. If the number is >= 1, then it is a fixed discount, otherwise we assume it is a percentage -->
    <!-- First, round down to the nearest integer -->
    [calculation temp cf7-hide precision:0 rounddown "raw_discount"]
    [calculation temp_2 cf7-hide precision:0 rounddown "raw_discount_2"]
    <!-- Then, normalize to either 0 or 1 -->
    [calculation is_fixed cf7-hide min:0 max:1 "temp"]
    [calculation is_fixed_2 cf7-hide min:0 max:1 "temp_2"]
    <!-- Calculate discount -->
    <label>Discount: $ [calculation discount "raw_discount * is_fixed + (subtotal * raw_discount * (1-is_fixed)) + raw_discount_2 * is_fixed_2 + (subtotal * raw_discount_2 * (1-is_fixed_2))"]</label>
    <!-- Calculate total -->
    <label>Total: $ [calculation total "subtotal - discount"]