> ## 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.

# Install and connect Magento 2

> [!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 LoyaltyLion module connects each website in your Magento store to a LoyaltyLion program. It sends the website's orders and customers to LoyaltyLion, shows your loyalty program on the storefront, and lets LoyaltyLion create reward codes in your store. [How the module works](https://developers.loyaltylion.com/docs/installation/magento-2/how-it-works.md) describes each part.

## Requirements

| Requirement | Supported |
| - | - |
| Magento | Magento Open Source 2.4.4 or later |
| PHP | 8.1 or later |
| Theme | Luma, a theme based on Luma, or a Hyvä theme |
| Checkout | Magento's own checkout, which a Hyvä theme serves as its fallback |

Not yet supported or tested:

* **Hyvä Checkout** isn't supported yet. A store with a Hyvä theme is supported when its checkout is Magento's own.
* **Adobe Commerce**, the paid edition, hasn't been tested with the module yet.
* Themes that are based on neither Luma nor Hyvä haven't been tested.

LoyaltyLion calls your store's REST API, so the store's base URL (**Stores > Configuration > General > Web**) must start with `https://` and point to an address that's reachable from the public internet. HTTP basic authentication on the store's OAuth and REST routes stops LoyaltyLion from connecting.

## Install the module

Run these in your Magento root directory, as the user that owns your Magento files:

```sh theme={null}
composer require loyaltylion/core:^2.0
bin/magento setup:upgrade
```

If your store runs in production mode, follow `setup:upgrade` with your usual deployment steps, such as `bin/magento setup:di:compile` and `bin/magento setup:static-content:deploy`.

`setup:upgrade` adds the module's `loyaltylion_outbox` table and a `loyaltylion_tier` customer attribute, and registers an integration named LoyaltyLion under **System > Extensions > Integrations**. Nothing reaches LoyaltyLion and nothing shows on the storefront until a website is connected.

## Check cron

The module's jobs run in a cron group of their own, `loyaltylion`, in a separate process, so a slow answer from LoyaltyLion never delays your store's other jobs.

| Job | Runs | What it does |
| - | - | - |
| `loyaltylion_send_outbox` | Every minute | Sends waiting orders and customers to LoyaltyLion |
| `loyaltylion_send_heartbeat` | Every hour, at minute 7 | Reports in to LoyaltyLion with the module and Magento versions, the theme, whether the integration is approved, and how much is waiting to be sent |

A crontab that runs `bin/magento cron:run` without `--group`, as `bin/magento cron:install` sets it up, runs this group too, so there's nothing to add. If your crontab runs named groups only, add the `loyaltylion` group to it, every minute:

```sh theme={null}
* * * * * php <magento-root>/bin/magento cron:run --group=loyaltylion
```

Without cron, nothing reaches LoyaltyLion. If a connected store doesn't report in for 6 hours, your LoyaltyLion dashboard shows **Your store stopped reporting to LoyaltyLion**.

## Connect your store

You need a LoyaltyLion program for the store, and a LoyaltyLion user that can manage its account.

**1. Activate the integration**

In Magento admin, go to **System > Extensions > Integrations** and select **Activate** next to LoyaltyLion, or **Reauthorize** if Magento shows that instead. Magento lists the resources the integration asks for. Allow them, and Magento opens LoyaltyLion in a new window.

**2. Choose the program**

Sign in to LoyaltyLion if it asks. The **Connect your store** page shows your store's address. Check it's your store, choose the program, then select **Connect store**.

If your store has more than one website, LoyaltyLion asks which one the program is for. Choose it and select **Connect website**.

**3. Read the result**

The **Store connected** page names the store, the program and, for a store with more than one website, the website.

* If it says your store now sends its orders and customers to LoyaltyLion, the website is set up, and the window closes by itself.
* If it asks you to enter the program's settings in Magento, follow [Enter the settings yourself](#enter-the-settings-yourself).

> [!NOTE]
>
> Your Loyalty Program is completely hidden from your customers until you choose
> to launch using the button on the LoyaltyLion admin dashboard.

While the program hasn't launched, the module keeps the website's orders and customers and sends them once it has.

### How LoyaltyLion sets up a website

LoyaltyLion sets the website up only when your store is at the address of the program's **Store URL** (in LoyaltyLion, **Settings > General**). It compares the host name alone and ignores `http` or `https`, a leading `www.`, the port and any path. If the two don't match, the store is still connected, and you enter the settings yourself.

To set the website up, LoyaltyLion:

1. Creates an API key for the program with the `read_customers`, `write_customers` and `write_orders` scopes, or reuses the one it made before.
2. Saves the four settings below in the website's configuration. The API key and SDK secret token are stored encrypted.
3. Cleans Magento's configuration and full page caches, so that cached pages pick up the LoyaltyLion script.

The module then reports in to LoyaltyLion.

### Enter the settings yourself

Go to **Stores > Configuration > LoyaltyLion > Connection** and switch the scope to the website. The settings are at website scope only, so the **Default Config** scope doesn't show them. Under **Connect this website to LoyaltyLion**, enter:

| Setting | Where to find it in LoyaltyLion |
| - | - |
| Site ID | The number after `/sites/` in the address bar while you're signed in |
| Site token | **Settings > General**, the **Token** under **Token and secret** |
| API key | [Create an API key](https://developers.loyaltylion.com/api-reference/authentication/api-keys.md#create-an-api-key) with the `read_customers`, `write_customers` and `write_orders` scopes |
| SDK secret token | **Settings > General**, under **SDK secret token** |

Save the configuration, then refresh the full page cache under **System > Cache Management**. Pages cached before you saved don't carry the LoyaltyLion script.

Each setting turns on part of the module:

* With a **Site token**, the storefront loads LoyaltyLion.
* With an **SDK secret token** as well, LoyaltyLion knows which shopper is signed in.
* With a **Site ID** and an **API key**, the module sends orders and customers, and checks free product rewards with LoyaltyLion.

## Stores with more than one website

Each Magento website connects to one LoyaltyLion program, and its settings are saved at that website's scope. To connect another website, give it a program of its own:

1. In Magento admin, select **Reauthorize** next to LoyaltyLion under **System > Extensions > Integrations**.
2. On **Connect your store**, choose the other program and select **Connect store**.
3. On **Choose a website**, choose the website and select **Connect website**.

**Choose a website** shows the program each website is connected to already. If you choose a website that another program is connected to, the website sends its new orders and customers to the program you're connecting instead.

## Check the connection

Run these in your Magento root directory.

`bin/magento loyaltylion:report` reports in to LoyaltyLion now, as the hourly job does, and prints what it sent. LoyaltyLion offers your store only the features the module last reported it can do, so run it after an install or an upgrade. `setup:upgrade` reports in too, once the website is connected. In `answer_by_website`, each connected website's ID has LoyaltyLion's answer:

* `200`: LoyaltyLion accepted the report.
* `401` or `403`: LoyaltyLion refused the website's API key. Check the **API key** setting.
* `0`: the store couldn't reach LoyaltyLion.

A website without a Site ID and an API key isn't listed.

`bin/magento loyaltylion:outbox:send` sends what's waiting now, as the job does each minute, and prints:

* `sent_now`: how many orders and customers met each outcome in this run, such as `sent`, `retry` or `held`.
* `waiting`: how many are still to be sent.
* `parked`: how many LoyaltyLion refused.
* `oldest_waiting_at`: when the oldest waiting one was noted.

`bin/magento loyaltylion:outbox:retry-parked` queues the parked orders and customers again. Run it once you've put right what LoyaltyLion refused them for, such as a wrong Site ID. Add `--website <id>` to queue one website's only. [Waiting, held and parked](https://developers.loyaltylion.com/docs/installation/magento-2/how-it-works.md#waiting-held-and-parked) explains each state.

The module logs what it couldn't do in the store's log, with lines that start `[LoyaltyLion]`.
