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

# Shopify tips & tricks

> Common questions, tips, and workflow guides for getting the most out of the Dema–Shopify integration.

## Discount tracking with compare-at price

### Why `compareAtPrice` alone is not enough

In Shopify, every product variant has a **Compare-at price** field (`compareAtPrice`). When set, Shopify displays it as a strikethrough "original" price alongside the current selling price — giving shoppers a visual discount indicator.

However, Shopify **does not include `compareAtPrice` in the order line item data** exposed through its Orders API. This means that when Dema syncs your orders, the raw order records contain no information about what the compare-at price was at the time of purchase — only the price the customer actually paid.

As a result, simply setting `compareAtPrice` on your product variants is not enough for Dema to calculate discount percentages or attribute revenue to sale pricing. The compare-at price must be explicitly captured at order time.

### The fix: save compare-at price as an order metafield via Shopify Flow

The solution is to use **Shopify Flow** to run an automation every time a new order is placed. The workflow reads the `compareAtPrice` from each line item's variant and saves it as an **order metafield** — a JSON object mapping each SKU to its original price. Dema (and any other downstream tool) can then read this metafield to compute accurate discount percentages.

#### Prerequisites

* Shopify **Basic** plan or higher (Flow is included on all paid plans).
* Admin access to **Apps → Shopify Flow**.
* `compareAtPrice` already set on the product variants you want to track (see [Setting compareAtPrice on variants](#setting-compareatprice-on-variants) below).

#### Setting up the workflow

<Steps>
  <Step title="Open Shopify Flow">
    In your Shopify admin, go to **Apps → Flow**. Click **Create workflow**.
  </Step>

  <Step title="Set the trigger: Order created">
    Click **Select a trigger** and choose **Order created**. This fires every time a new order is placed in your store.
  </Step>

  <Step title="Add the action: Update order metafield">
    Click the **+** button after the trigger, then select **Update order metafield**. Configure the metafield with the following settings:

    | Field         | Value              |
    | ------------- | ------------------ |
    | **Namespace** | `custom`           |
    | **Key**       | `compare_at_price` |
    | **Type**      | `JSON`             |
  </Step>

  <Step title="Paste the Liquid template as the value">
    In the **Value** field, switch to the Liquid / template editor and paste the following code exactly:

    ```liquid theme={null}
    {% capture lineItems_output %}
    {% assign seen_skus = '' %}
    {% for lineItems_item in order.lineItems %}
    {% if lineItems_item.variant.compareAtPrice > 0 %}
    {% unless seen_skus contains lineItems_item.sku %}
    , "{{ lineItems_item.sku }}": "{{ lineItems_item.variant.compareAtPrice }}"
    {% assign seen_skus = seen_skus | append: lineItems_item.sku | append: ',' %}
    {% endunless %}
    {% endif %}
    {% endfor %}
    {% endcapture %}
    {
    {{ lineItems_output | remove_first: ', ' }}
    }
    ```

    This only populates when at least one line item has a `compareAtPrice` set — which typically only happens during sale or promo periods. On regular orders it produces an empty JSON object `{}`.
  </Step>

  <Step title="Save and turn on the workflow">
    Click **Save**, then toggle the workflow to **On**. It will run on every new order from this point forward.
  </Step>
</Steps>

<Note>
  This workflow only applies to **new orders** created after it is enabled. Historical orders will not be backfilled with compare-at price data.
</Note>

#### What you get

Each order will have a metafield at `custom.compare_at_price` containing a JSON object that maps each discounted SKU to its original price at the time of purchase:

```json theme={null}
{
  "SKU-001": "89.00",
  "SKU-002": "129.00"
}
```

If no items had a compare-at price set, the metafield will be `{}`.

#### How the Liquid code works

| Part                                                     | What it does                                                                                                                                                      |
| -------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `capture lineItems_output`                               | Stores the loop output as a string so the leading comma can be cleaned up afterwards                                                                              |
| `assign seen_skus = ''`                                  | Initialises an empty string used to track which SKUs have already been written, preventing duplicate JSON keys if the same variant appears on multiple line items |
| `for lineItems_item in order.lineItems`                  | Loops through every line item on the order                                                                                                                        |
| `if lineItems_item.variant.compareAtPrice > 0`           | Filters to only items that have a compare-at price set (i.e. they are on sale)                                                                                    |
| `unless seen_skus contains lineItems_item.sku`           | Skips the SKU if it has already been written, ensuring each key appears only once in the JSON output                                                              |
| `, "{{ lineItems_item.sku }}": "{{ ... }}"`              | Writes each matching item as a JSON key-value pair with a leading comma                                                                                           |
| `seen_skus \| append: lineItems_item.sku \| append: ','` | Records the SKU as seen so subsequent duplicates are skipped                                                                                                      |
| `remove_first: ', '`                                     | Strips the leading comma so the resulting JSON is valid                                                                                                           |
| Outer `{ }`                                              | Wraps everything as a JSON object                                                                                                                                 |

#### Where you can use the metafield

Once the workflow is live, `custom.compare_at_price` is available on every order via `order.metafields.custom.compare_at_price`. You can use it in:

* **Dema reporting** — Dema reads this metafield automatically to compute discount rates and full-price vs. sale revenue splits.
* **Integrations** — pass the original price to any downstream system (ERP, data warehouse, BI tool) that needs to know the pre-discount value.
* **Order confirmation emails** — reference `order.metafields.custom.compare_at_price` in your email templates to show customers how much they saved.
* **Other Shopify Flow workflows** — trigger further automations based on whether an order contained discounted items.

***

### Setting compareAtPrice on variants

Before the Flow workflow can capture compare-at prices, the field must be populated on your product variants. If your catalog already has this set, no action is needed.

#### Single product

1. In your Shopify admin, go to **Products** and open the product.
2. In the **Variants** section, click the variant you want to edit.
3. Under **Pricing**, enter the original price in the **Compare-at price** field.
4. Click **Save**.

#### Bulk edit

1. Go to **Products**, select the products to update, then click **Edit products**.
2. Click **Columns** and enable **Compare-at price**.
3. Fill in the compare-at price for each row, then click **Save**.

#### CSV import

1. Export your catalog: **Products → Export → All products → CSV for Excel, Numbers, or other spreadsheet applications**.
2. Populate the `Variant Compare At Price` column for each relevant row.
3. Re-import: **Products → Import → upload your updated CSV → Import products**.
