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

# How the Magento 2 module works

> [!WARNING]
>
> This page describes version 2.0 of the LoyaltyLion module for Magento 2, which
> isn't released yet, and applies from the 2.0 release. Until then, install the
> current module by following [Adobe Commerce
> 2](https://developers.loyaltylion.com/docs/installation/adobe-commerce-2.md).

The module tells LoyaltyLion about your orders and customers, loads LoyaltyLion on your storefront, and puts claimed free products in the cart. LoyaltyLion creates reward codes in your store through the module's integration. The module holds no rules about points or rewards: those stay in LoyaltyLion.

## What the module adds to your store

* A table, `loyaltylion_outbox`, that holds the orders and customers waiting to be sent.
* A customer attribute, `loyaltylion_tier` (**LoyaltyLion tier**), that holds a member's tier. It's shown in the customer grid.
* Two cron jobs in their own group, `loyaltylion`. See [Check cron](https://developers.loyaltylion.com/docs/installation/magento-2/install.md#check-cron).
* An integration named LoyaltyLion, under **System > Extensions > Integrations**.
* Settings under **Stores > Configuration > LoyaltyLion > Connection**, at website scope.
* Three commands: `loyaltylion:report`, `loyaltylion:outbox:send` and `loyaltylion:outbox:retry-parked`. See [Check the connection](https://developers.loyaltylion.com/docs/installation/magento-2/install.md#check-the-connection).

## Orders, customers and refunds

When an order or a customer is saved, the module notes it in `loyaltylion_outbox` as part of the same save. Every minute, the `loyaltylion_send_outbox` job reads each noted order or customer as it is at that moment and sends it to LoyaltyLion. Nothing a shopper does waits for LoyaltyLion: an order is placed the same way whether LoyaltyLion answers or not.

The module notes:

* An order when it's placed, invoiced, refunded with a credit memo or cancelled. Each order carries its lines, so product and category rules and points modifiers apply to it.
* A customer when their details change, such as their email, name or customer group, and when they're deleted.
* A registration, when a shopper creates an account on the storefront or through GraphQL. A customer that an admin, an import or the REST API creates isn't a sign-up, so it doesn't earn the sign-up rule's points.
* A newsletter subscription, when a signed-in customer subscribes.

When a guest later creates an account with the same email and signs in on the storefront, their guest order's points move to the account.

With **Show LoyaltyLion on the order confirmation page** on, the confirmation page sends its order straight away rather than waiting for the next run.

### Waiting, held and parked

Each noted order or customer is in one of three states:

* **Waiting**: not yet accepted by LoyaltyLion. If LoyaltyLion can't be reached or asks the store to try later, the module tries again after 1 minute, then 2, then 4, doubling up to every 6 hours, for as long as it takes.
* **Held**: the website has no Site ID or API key, the program hasn't launched, or LoyaltyLion refused the API key. The module holds everything waiting for that website and tries again after 15 minutes.
* **Parked**: LoyaltyLion refused that order or customer. The module logs why and keeps the reason in `last_error` in `loyaltylion_outbox`. A parked order or customer is sent again when its order or customer changes, or when you run `bin/magento loyaltylion:outbox:retry-parked`.

Each run stops after 40 seconds and the next run carries on. If LoyaltyLion doesn't answer twice in a row, the run stops and leaves the rest for the next one.

## Reward codes

When a shopper claims a reward that gives a code, LoyaltyLion creates the code as a coupon on a cart price rule in your store, under **Marketing > Cart Price Rules**. The rule's name starts with `[LoyaltyLion]`, followed by the reward's title, and its description reads "Automatically generated by LoyaltyLion - do not modify". The shopper sees the rule's label at checkout, and a referral code's label reads "Referral discount".

A reward's minimum spend becomes a condition on the rule's subtotal. A change to what a reward's code gives, such as a new minimum spend, applies to codes claimed after the change, which LoyaltyLion puts on a new rule. Codes claimed before it keep their rule. Make changes to a reward in LoyaltyLion rather than to its rules in Magento, because LoyaltyLion sets the rules' **Priority** and **Discard subsequent rules** from the reward. [Discount combinations](https://developers.loyaltylion.com/docs/installation/magento-2/discount-combinations.md) explains how the reward's rules combine with your store's own.

When a reward is refunded in LoyaltyLion, its code is deleted from the store.

On the **Reward settings** page in LoyaltyLion, **How voucher codes are applied** decides whether a claimed code goes onto the shopper's cart by itself (**Automatic**) or the shopper pastes it (**Manual**). A Magento Open Source cart holds one coupon code at a time, so a code applied this way replaces one the shopper entered.

## Free products in the cart

With **Add claimed free products to the cart** on, a signed-in shopper who claims a free product gets the product in their cart at no charge, with the line labelled "Reward: Free with your points". The setting is off by default. With it off, the module adds nothing to the cart, and a free product voucher gives the shopper its code to enter.

* The line stays at a quantity of 1 and a price of 0. The rest of the cart can still take the shopper's own coupon code.
* The product must be a simple, virtual or downloadable product with no required custom options. For a configurable product, the reward gives one of its variants.
* Before adding the product, the module asks LoyaltyLion whether the claim can be used and checks that the rest of the cart meets the reward's minimum spend or number of paid items.
* When the order is placed, the module asks again. If the claim has ended or the cart no longer meets the requirement, the module removes the line and stops the order, and the shopper sees: "A reward in your cart is no longer available, so we removed it. Check your order and place it again."
* If the shopper removes the line, the reward isn't used up.
* A free product code that has been on an order can't be added to another cart.
* While the cart holds a reward line, **Check Out with Multiple Addresses** isn't available.

## On your storefront

The module loads LoyaltyLion's [SDK](https://developers.loyaltylion.com/sdk/overview.md) on every storefront page of a connected website, and signs in the shopper who's signed in to your store. It reads who the shopper is from Magento's private content, through a customer data section named `loyaltylion-shopper`, so pages served from the full page cache still show each shopper their own program. The module adds LoyaltyLion's hosts to the Content Security Policy of the pages it loads on.

On the checkout, the SDK loads only while **Show LoyaltyLion on the checkout's payment step** is on.

The **Storefront** settings, under **Stores > Configuration > LoyaltyLion > Connection**, set where else your program shows. Nothing shows on a website that isn't connected, whatever these say.

| Setting | Default | What it does |
| - | - | - |
| Add claimed free products to the cart | No | See [Free products in the cart](#free-products-in-the-cart) |
| Show the loyalty page | Yes | Serves the [loyalty page](https://developers.loyaltylion.com/sdk/embeddable-components/loyalty-page.md) at the address below. A CMS page of your own at the same address is shown instead |
| Loyalty page address | `loyalty` | The path after your store's address |
| Link the loyalty page from the main menu | No | Adds the loyalty page to the top menu |
| Show Rewards in the customer's account | Yes | Adds a **Rewards** page to the account menu. A shopper who isn't signed in is sent to sign in first |
| Show LoyaltyLion on the checkout's payment step | Yes | Adds a place on the payment step for LoyaltyLion's block, and loads the SDK on the checkout |
| Show LoyaltyLion on the order confirmation page | Yes | Adds LoyaltyLion's block to the order confirmation page |

To show other parts of your program, add their elements to a CMS page or block, as the [SDK components](https://developers.loyaltylion.com/sdk/embeddable-components/loyalty-page.md) describe.

### Headless storefronts

A headless storefront signs a shopper in to the SDK with the `loyaltylion_auth` field on the GraphQL `customer` query:

```graphql theme={null}
query {
  customer {
    loyaltylion_auth {
      customer_id
      email
      date
      token
    }
  }
}
```

Pass these to `loyaltylion.authenticateCustomer`, as [Customer authentication](https://developers.loyaltylion.com/sdk/customer-authentication.md) describes. The field is null when the website isn't connected, and Magento refuses the query for a shopper who isn't signed in.

## What LoyaltyLion calls in your store

LoyaltyLion calls your store's REST API with the access the LoyaltyLion integration was given. The integration asks for these resources:

| Why | Resources |
| - | - |
| Creating reward codes as cart price rules and coupons | `Magento_Backend::marketing`, `Magento_CatalogRule::promo`, `Magento_SalesRule::quote` |
| Reading orders, including the hourly check that it can reach your store | `Magento_Sales::sales`, `Magento_Sales::sales_operation`, `Magento_Sales::sales_order`, `Magento_Sales::actions`, `Magento_Sales::actions_view` |
| Reading products and categories, for product rewards and product rules | `Magento_Catalog::catalog`, `Magento_Catalog::catalog_inventory`, `Magento_Catalog::products`, `Magento_Catalog::categories` |
| Reading websites and customer groups | `Magento_Backend::stores`, `Magento_Backend::stores_settings`, `Magento_Backend::store`, `Magento_Customer::customer`, `Magento_Customer::group` |
| Saving a website's LoyaltyLion settings when you connect it | `Magento_Config::config`, `Loyaltylion_Core::config` |
| Writing a member's tier to their customer | `Magento_Customer::manage` |

The module adds one REST route, `PUT /V1/loyaltylion/websites/:websiteId/connection`, which needs the `Loyaltylion_Core::config` resource. It saves a website's settings when you [connect your store](https://developers.loyaltylion.com/docs/installation/magento-2/install.md#connect-your-store), and again when you rotate the program's SDK secret token in LoyaltyLion.

## When LoyaltyLion and your store can't reach each other

**Your store can't reach LoyaltyLion.** Orders and customers wait in the outbox and are sent when LoyaltyLion answers again. Checkout carries on as normal. A free product can't be added to a cart while LoyaltyLion can't be reached, but a free product already in a cart stays there and its order goes through.

**LoyaltyLion can't reach your store.** Every hour, LoyaltyLion asks your store's API for one order. If the store refuses (HTTP 401 or 403), LoyaltyLion stops calling it at once. If the store goes 24 hours without a usable answer, LoyaltyLion stops calling it then. That covers no answer, a server error, and an answer that isn't from the API, such as a maintenance page. Either way, your LoyaltyLion dashboard shows **LoyaltyLion can't reach your store's API**, and reward codes can't be created. To restore access, put right what blocked LoyaltyLion, then select **Reauthorize** next to LoyaltyLion under **System > Extensions > Integrations** and connect the store again.

**The integration needs approval again.** Magento withdraws its approval when a new version of the module asks for different resources. Until you approve it again, your LoyaltyLion dashboard shows **LoyaltyLion needs approval in Adobe Commerce**, and reward codes can't be created. Select **Reauthorize** next to LoyaltyLion and connect the store again.

**The module stops reporting.** If a connected store doesn't report in for 6 hours, your LoyaltyLion dashboard shows **Your store stopped reporting to LoyaltyLion**. Check that the store's cron runs the `loyaltylion` group.
