# Reward customers for repeat coffee purchases

> Quick-service restaurants and coffee shops use stamp cards as a simple but effective way to encourage repeat purchases and boost revenue. With recurring on completion achievements, you can implement a stamp card engine that automatically rewards customers with more of what they love, such as a free coffee for frequent coffee buyers.

> For the complete documentation index, see [llms.txt](https://docs.talon.one/llms.txt).

In this tutorial, let's create a coffee campaign using recurring on completion achievements
to reward customers for repeat purchases:
- **Goal**: Increase coffee orders by offering rewards.
- **Duration**: The promotion runs on an ongoing basis with no fixed end date.
- **Reward**: For every six coffees purchased, customers receive one free coffee
  added to their cart. For example, if a customer purchases `12` coffees in a single
  session, they complete the achievement twice and receive two free coffees.

## Set up the achievement and rules

<StepsContainer>

### Prepare your environment

Create a
[standard campaign](/docs/product/campaigns/overview#standard-campaigns)
named `Coffee Campaign` with the **Achievements** feature enabled.

To set up the campaign, we first create a cart item filter scoped to coffee
products, then create an achievement, and
[define two rules](/docs/product/rules/create-and-manage-rules#create-a-rule)
in this campaign.

### Create the achievement

Create a
[recurring-on-completion achievement](/docs/product/achievements/create-achievements.md)
to track the customer's coffee purchases:

- Set the **Name** as `buy6CoffeesGet1Free`.
- Set the **Target** to `6`. This value represents the number of coffees a
  customer must purchase to complete the achievement.

For every purchase the customer makes, the quantity of coffee items gets added
as progress to the achievement. When the customer's progress reaches `6`, the
achievement completes and resets. Let's set up the rules to track progress and
deliver the reward.

### Create a cart item filter

Restaurants and coffee shops often sell other products alongside coffee, such as
bagels, cakes, and smoothies. To ensure only coffee purchases count toward the
reward, let's filter the customer's cart for coffees only.

#### Cart item filter

Create a [cart item filter](/docs/product/rules/cart-item-filters/manage-cart-item-filters#create-a-cart-item-filter)
named `coffeeFilter` that includes all items whose `category` is `coffee`.

- **Start with this list of cart items**: `all items in cart`.
- Select **Filter items by conditions**:
  **Filter items when** `all` of these are true:
  <Attribute name="Item category (Item)" type="builtin"/> `contains` `coffee`.
- **Save as**: `coffeeFilter`.

### Create a rule to count coffee purchases

Let's create a rule that increases the customer's progress in the achievement by
the number of coffees purchased in each session. We can name the rule `Count
coffees purchased`. For example, if a customer buys four coffees in a session,
this rule increases their progress by `4`.

This rule checks whether the Rule Engine has triggered a `Customer Session Closing` event,
confirming that the customer completed a purchase order and the
[session](/docs/dev/concepts/entities/customer-sessions.md#customer-session-states)
is in `closed` state.

If the condition is true, the Rule Engine updates the customer's progress in
the achievement by the quantity of coffee items in the session, as determined
by the cart item filter.

:::note
Ensure that the coffee cart item's `category` is set to `coffee` for this rule
to evaluate as expected.
:::

#### Rule 1: Conditions

When **all** of these are true:

- **Check for event types and custom event values**:
  - **Event type**: `Customer session Closing`

#### Rule 1: Effects

- **Update customer progress**:
  - **Achievement name**: `buy6CoffeesGet1Free`
  - **Update progress by**:
    1. Click **Add an attribute** (  ).
    1. Select the <Attribute name="Item Count (coffeeFilter)" type="filter"/>
       attribute.

### Create a rule to award a free coffee

Let's create a second rule that checks if the achievement was completed and
awards the customer a free coffee for every six coffees purchased. This rule
adds a free coffee item to the cart with a quantity that matches the number of
times the achievement was completed in the session. We can name the rule `Award
free coffee for each achievement completion`.

For example, if a customer buys `12` coffees, the achievement completes twice,
and this rule adds two free coffees to their cart.

The [built-in attribute](/docs/dev/concepts/attributes#built-in-attributes)
<Attribute name="Achievement progress completion count in Session"
type="builtin"/> tracks how many times a customer completes the achievement in a
session. This count determines how many free coffees the customer receives.

:::tip
You can also add a [notification
effect](/docs/product/rules/effects/use-effects#create-a-notification) to inform
customers when they earn a reward to enhance the customer experience.
:::

#### Rule 2: Conditions

- **Has completed an achievement**:
  - **Achievement name**: `buy6CoffeesGet1Free`

#### Rule 2: Effects

- **Add free item**:
  - **Item**: `coffee_001`
  - **Discount name**: `Free coffee on six purhcased coffees`
  - **Quantity**:
    1. Click **Add an attribute** (  ).
    1. Select the <Attribute name="Achievement progress completion count in Session" type="builtin"/> attribute.

- *(Optional)* **Create notification**:
  - **type**: `Info`
  - **Title**: `Free coffee earned!`
  - **Body**: `Dear customer, you've earned` <Attribute name="Achievement progress completion count in Session" type="builtin"/> `free coffees as a reward for continuing to buy with us. Enjoy!`

</StepsContainer>

## Related pages

- [Achievements](/docs/product/achievements/overview.md)
- [Create achievements](/docs/product/achievements/create-achievements.md)
