> ## Documentation Index
> Fetch the complete documentation index at: https://voucherify-mintlify-a4e04423.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# Create dynamic formulas

> How to create dynamic discount and loyalty point formulas

Use dynamic formulas to calculate discounts, product units, replacement units, or loyalty points based on order, customer, item, metadata, or campaign-related values.

<Note>
  For supported operands, functions, operators, and expression syntax, see [Dynamic formula reference](/optimize/dynamic-formula-reference).
</Note>

The formula builder helps you create formulas by typing them directly, selecting variables, metadata, functions, and operators, using templates, or working in the diagram view. Templates provide starting points for common logic, such as calculations based on order amount, customer metadata, or order currency.

The formula field shown in the builder depends on the selected discount type, product discount setup, or earning rule configuration. Before you create a formula, the required value field uses the label of the configured value. After you create a formula, this field changes to **Fallback** and is used when the formula cannot be calculated.

<Tabs>
  <Tab title="Amount formulas">
    Amount formula fields are shown when the selected configuration calculates a monetary value.

    Formula fields:

    * **Discount Amount formula**: calculates the discount amount
    * **Fixed amount formula**: calculates the fixed discount amount
    * **New price formula**: calculates the new product price

    Example formula:

    ```wrap theme={null}
    IF(ORDER_AMOUNT > 200;ORDER_AMOUNT * 0.15;ORDER_AMOUNT * 0.05)
    ```
  </Tab>

  <Tab title="Percent formulas">
    Percent formula fields are shown when the selected configuration calculates a percentage value.

    Formula field:

    * **Percent of discount formula**: calculates the discount percentage

    Example formula:

    ```wrap theme={null}
    IF(CUSTOMER_METADATA("tier") is "VIP";25;10)
    ```
  </Tab>

  <Tab title="Quantity formulas">
    Quantity formula fields are shown when the selected configuration calculates a unit quantity.

    Formula fields:

    * **Units quantity formula**: calculates the number of discounted units
    * **Free units quantity formula**: calculates the number of free units
    * **Replacement Quantity per Unit formula**: calculates the replacement quantity per unit

    Example formula:

    ```wrap theme={null}
    IF(ORDER_UNITS_QUANTITY > 5;FLOOR(ORDER_UNITS_QUANTITY / 3;0);1)
    ```
  </Tab>

  <Tab title="Points formulas">
    Points formula fields are shown when the selected earning rule configuration calculates loyalty points.

    Formula field:

    * **Points formula**: calculates the number of awarded loyalty points

    Example formula:

    ```wrap theme={null}
    IF(ORDER_AMOUNT > 500;ROUND(ORDER_AMOUNT * 1.5;0);ROUND(ORDER_AMOUNT;0))
    ```
  </Tab>
</Tabs>

## Type a formula

Type or paste a formula directly into the formula field.

Use the supported expression syntax. For example, to return a different value depending on the order amount, enter:

```wrap theme={null}
IF(ORDER_AMOUNT > 100;15;5)
```

## Add formula elements

Select the formula field to show the formula toolbar.

Use the toolbar to add formula elements.

Available controls:

* **Add variable**: Adds predefined operands, such as order, customer, or item values.
* **Add metadata**: Adds metadata operands.
* **IF**: Adds conditional logic.
* **SWITCH**: Adds switch logic.
* **+**: Adds an operator.
* **Add FUNCTION**: Adds a function.
* **Templates**: Opens predefined formula templates.

After adding an element, fill in the required arguments in the formula field, such as metadata keys, comparison values, or values returned by conditional logic.

## Use templates

Open **Templates** and click a template.

Available templates:

* **Based on order amount**: Creates formula logic based on the order amount.
* **By customer metadata condition**: Creates conditional formula logic based on customer metadata.
* **By customer metadata value**: Creates formula logic that uses a customer metadata value.
* **By order currency**: Creates formula logic based on the order currency.

Templates add predefined formula logic that you can modify after inserting it. After selecting a template, update the generated values and conditions to match your promotion or loyalty logic.

## Use the diagram view

Click **Show diagram** to open the formula in a diagram view.

If the formula field is empty, the diagram view shows **Click to add** and template shortcuts. Use **Click to add** to start creating the formula visually, or select one of the displayed templates.

If a formula is already defined, the diagram view shows the formula as connected nodes and keeps the editable expression below the diagram.

In the diagram view, you can also use:

* **Formula toolbar and inline formula editor**: Add or edit formula elements without leaving the diagram view.
* **Zoom controls**: Adjust the diagram view.

You can use the diagram view to inspect existing logic and add new conditions or cases directly to the formula.

## Format, validate, and save

Use **Format** to format the expression.

Available options:

* **Format now**: Formats the current formula
* **Auto-format**: Reformats the formula automatically while editing

To remove the formula, click **Clear**.

Review the validation message below the formula editor. If the formula is correct, the builder shows **Formula is valid**.

Click **Save** to save the discount configuration or earning rule.

After the campaign is saved, you can edit the formula by editing the campaign from the campaign view.
