> ## Documentation Index
> Fetch the complete documentation index at: https://docs.checkoutrules.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Branching (if else)

> Learn how to create and use an if, else if, else rule structure to handle multiple conditions in your rules.

<iframe className="w-full aspect-video rounded-xl" src="https://www.youtube.com/embed/E_ufsYrU5ko?si=dSTtPQSFtjcxzl3y" title="BeSure Branching" style={{ border: 0 }} allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowFullScreen />

## Overview

Usually when a rule has more than one blocks, then each block is executed in a sequence from top to bottom, and if conditions for any block are met, then the actions for that block are executed. So, each rule can have multiple actions. But some times we do not want this behavior, we only want one block to execute and then the rule should stop. This is where branching comes in.

Branching allows you to build rules using multiple condition blocks that follow **if → else if → else** logic. Each block is checked in **sequence**, and as soon as one block matches, its actions are executed, and the rule stops. This ensures that **only one block runs**, preventing overlapping or conflicting actions. This is a very powerful feature and can be used to create complex rules with multiple conditions.

> Here is an example of a branching rule with three blocks. Here, we want to apply different discounts based on the product tag and we have a default discount of 5% off on all shipping in the case customer is not eligible for any of the other discounts. Only one block will execute based on the conditions and then the rule will stop.

<Note>
  The key condition here is [Previous all conditions
  invalid](/guides/en/essentials/condition-sets/condition-set-1#previous-all-conditions-invalid)
  in second and third blocks, which ensures only one block will execute based on
  the conditions and then the rule will stop. Using this condition you can use
  branching in any rule.
</Note>

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/all-blocks.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=72a534141bcd8bc66efb87259c3e3f70" alt="All Blocks" width="1091" height="2116" data-path="images/advanced/branching/all-blocks.webp" />

This is how this rule will execute:

<Steps>
  <Step title="If product tag is sport and order subtotal amount is greater than or equal to 33">
    Then 20% off shipping will be applied and the rule will stop.
  </Step>

  <Step title="Else if customer tag is VIP">
    Then 50% off shipping will be applied and the rule will stop.
  </Step>

  <Step title="Else (default catch all)">
    Then 5% off shipping will be applied.
  </Step>
</Steps>

## Explanation

### First condition block (if)

* The system starts with the first condition block.
* This block can include multiple **sub-conditions** combined with **AND** or **OR**.
* If the overall block condition is fulfilled, its **“Then” action** runs (e.g., apply 20% discount).
* Once it runs, the system does not check the next blocks.

> **Example:** Product tag has value of **sport** *AND* Order subtotal amount ≥ **33** *OR* Product tag has value of **shoes** → Apply 20% OFF on all shipping

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/if-condition.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=a88329457c2bbd8a148e3231c1bc6119" alt="If Condition" width="1079" height="783" data-path="images/advanced/branching/if-condition.webp" />

### Else if condition block(s)

* If the first block does not match, the system checks the next block(s).
* An **Else If** block can contain multiple **sub-conditions** using **AND** or **OR**, and it must include the **"Previous all conditions invalid"** condition as a subcondition.
* There may be one or more blocks in sequence. However, if the **"Previous all conditions invalid"** condition is not present, the block will not work as an **Else If** and will behave as a regular **If** block.
* If the overall block condition for any block is fulfilled, its **“Then” action** runs (e.g., apply 50% discount), and evaluation stops.

> **Example:** Previous all conditions invalid AND Customer tag has value of **VIP** → Apply 50% off on all shipping

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/else-if-condition.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=7b086b9b2b65b76d5b3dd7b20f2e9b8a" alt="Else If Condition" width="1092" height="657" data-path="images/advanced/branching/else-if-condition.webp" />

### Fallback block (else)

* If none of the previous blocks match, you can add a fallback block with a condition of [Previous all conditions invalid](/guides/en/essentials/condition-sets/condition-set-1#previous-all-conditions-invalid).
* This acts as the **Else (default catch all)** and ensures that there is always a default **“Then” action** (e.g., apply 5% discount), when no previous blocks were executed.

> **Example:** Previous all conditions invalid → Apply 5% OFF on all shipping

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/else-condition.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=5c89f6422b905a1ac9ed0d065e653021" alt="Else Condition" width="1086" height="553" data-path="images/advanced/branching/else-condition.webp" />

<Tip>
  You should arrange the blocks in the order of highest priority to lowest
  priority. The first block is the highest priority and the last block is the
  lowest priority.
</Tip>

## Example use case

Let’s say you want to apply different shipping discounts based on **product tags**.

### Rule setup

**Block 1 (if)** → *Product tag has value of `sport`* → Apply **20%** off on all shipping (highest priority).

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/20-percent-when-sport.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=a97886dd9ec1c5e7c6d97c950a1f9480" alt="20 Percent When Sport" width="699" height="361" data-path="images/advanced/branching/20-percent-when-sport.webp" />

**Adding the next block**\
To add a new block after the first one, you can either:\
Click the **“Add new block”** button in the rule builder.

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/add-new-block.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=13c7c37ac24c8dffe68ab08f12479732" alt="Add New Block" width="1099" height="102" data-path="images/advanced/branching/add-new-block.webp" />

Or, [copy the previous block](/guides/en/advanced/block-replication) and **paste it below** using the **paste icon**, then modify its conditions and actions.

**Block 2 (else if)** → *Product tag has value of `winter`* AND *Previous all conditions invalid* → Apply **50% off on all shipping**

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/50-percent-when-winter.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=03c579fdd7a5e7531160b2d15a63a169" alt="50 Percent When Winter" width="750" height="452" data-path="images/advanced/branching/50-percent-when-winter.webp" />

**Block 3 (else)** → *Previous all conditions invalid* → Apply **5% off on all shipping** (default discount).

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/5-percent-when-previous-all-conditions-invalid.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=502562c7d54188ebf813af1423c5e116" alt="5 Percent When Previous All Conditions Invalid" width="697" height="362" data-path="images/advanced/branching/5-percent-when-previous-all-conditions-invalid.webp" />

Here is how the whole rule setup will look.

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/example-all-blocks.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=1bc0d8e0817caf7bfa9033b795cf00ef" alt="Example All Blocks" width="1093" height="2022" data-path="images/advanced/branching/example-all-blocks.webp" />

### Execution flow

The system starts with **Block 1**. If it matches (product tag = `sport`), **20% off shipping** is applied, and the rule ends.

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/20-percent-when-sport-checkout.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=b7ae092b666adb6cba4c9b32f43a648d" alt="20 Percent When Sport Checkout" width="1295" height="599" data-path="images/advanced/branching/20-percent-when-sport-checkout.webp" />

If Block 1 does not match, it checks **Block 2**. If product tag = `winter`, then **50% off shipping** applies, and the rule ends.

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/50-percent-when-winter-checkout.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=9e2938f5332e5b1eeabd2d9d7d13444f" alt="50 Percent When Winter Checkout" width="1248" height="608" data-path="images/advanced/branching/50-percent-when-winter-checkout.webp" />

If neither Block 1 nor Block 2 matches, **Block 3** executes. Since it uses **Previous all conditions invalid**, it acts as a **default catch all**, applying **5% off shipping**.

<img src="https://mintcdn.com/storespark/RDyGA09NSLSSmL-r/images/advanced/branching/5-percent-when-previous-all-conditions-invalid-checkout.webp?fit=max&auto=format&n=RDyGA09NSLSSmL-r&q=85&s=1ed8d476ad828b88457e0da540203a1a" alt="5 Percent When Previous All Conditions Invalid Checkout" width="1214" height="604" data-path="images/advanced/branching/5-percent-when-previous-all-conditions-invalid-checkout.webp" />
