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 thecompareAtPrice 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.
compareAtPricealready set on the product variants you want to track (see Setting compareAtPrice on variants below).
Setting up the workflow
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.
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 |
Paste the Liquid template as the value
In the Value field, switch to the Liquid / template editor and paste the following code exactly: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 {}.This workflow only applies to new orders created after it is enabled. Historical orders will not be backfilled with compare-at price data.
What you get
Each order will have a metafield atcustom.compare_at_price containing a JSON object that maps each discounted SKU to its original price at the time of purchase:
{}.
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_pricein 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
- In your Shopify admin, go to Products and open the product.
- In the Variants section, click the variant you want to edit.
- Under Pricing, enter the original price in the Compare-at price field.
- Click Save.
Bulk edit
- Go to Products, select the products to update, then click Edit products.
- Click Columns and enable Compare-at price.
- Fill in the compare-at price for each row, then click Save.
CSV import
- Export your catalog: Products → Export → All products → CSV for Excel, Numbers, or other spreadsheet applications.
- Populate the
Variant Compare At Pricecolumn for each relevant row. - Re-import: Products → Import → upload your updated CSV → Import products.

