# Architecture of the Rule Builder

The rule builder helps determine if a discount applies to items in your shopping cart. We'll walk through how it works, the logic behind it, advantages, limitations, and provide a flow diagram for better understanding.

***

### **What are Conditions?**

**Conditions** are the building blocks of the rule builder. Each condition works by combining the property, condition and test value to set up a rule.

An example where the condition is set up as follows

1. Property -> Product ID
2. Condition -> matching one of&#x20;
3. Test value -> The Collection Snowboard: Hydrogen

Basically in this case we built a rule that checks, if the item's product id matches against a set of items that we have selected, in this case the \`Hydrogen\` Snowboard.

***

### How the Rule Builder Checks Discounts (Product Discounts)

#### Standard Behaviour

1. **Fetch All Discounts and Rules**: The rule builder retrieves all the discount configurations and their associated rules.
2. **Apply Rules to Each Item**: For each item in the cart, the rule builder checks all the rules.
3. **Item Meets All Rules**: The rule builder evaluates if the item in the cart meets all the configured rules.&#x20;
4. **Apply Relevant Discounts**: If the item meets all the rules, the relevant discounts are applied based on the discount application strategy (`FIRST`/`MAX`/`ALL`).
   1. Note that if more than one discount is applicable to the eligible lines, then Shopify applies the first discount from the applicable discounts.
5. **Repeat for Each Item**: The process repeats for the next item in the cart until all items are evaluated.

#### Behaviour with When-Then flag

1. **Fetch All Discounts and Rules**: The rule builder retrieves all the discount configurations and their associated rules.
2. **Apply Rules to Each Item**: For each item in the cart, the rule builder checks all the rules.
3. **If the When condition evaluates to TRUE**: The rule builder checks if the **then** condition is matched by any item in the cart and applies the discount on the matched lines based on the line application strategy and discount application strategy configured.

{% hint style="info" %}
Fixed amount and Percentage off is applied on the line level meaning that the discount type is applied for each eligable item in cart. (ie - they are "Fixed Per Item Discount")\
\
If you want to use Fixed total discount / Fixed total percentage discount - you should be creating a Order discount.
{% endhint %}

***

### How the Rule Builder Checks Discounts (Order Discounts)

The only difference in the logic between product and order discount evaluation criteria is that it checks if all the rules configured for a particular discount(s) are met for the whole cart, then the discount is applied on the cart level based on the discount application strategy. \
\
Discount application strategy available in the order discount include "Only apply the first discount with conditions that are satisfied." (FIRST) and "Only apply the discount that offers the maximum reduction." (MAXIMUM)

{% hint style="info" %}
The order discount function does not target individual line items in the cart, instead these discounts are applied to the entire cart.&#x20;
{% endhint %}

***

### What are the Advantages of using the Rule Builder&#x20;

**Flexibility:**

* Create a variety of rules to fit different discount scenarios.
* Combine rules using simple AND/OR logic to develop complex discount structures not possible with presets.
* Customize discounts to align with specific marketing strategies.

**Targeted Discounts:**

* Apply discounts to specific items based on detailed conditions such as customer tags, product types, or purchase history.
* Tailor discounts for individual customer segments, enhancing personalized marketing efforts.
* Utilize advanced targeting to optimize promotional effectiveness and customer satisfaction.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://help.dollarlabs.io/dollarlabs-ultimate-discounts/architecture-of-the-rule-builder.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
