An easy way to access the customer's cart using Superfans SDK.
Description
The cart variable provides access to the current customer’s shopping cart data within the Superfans SDK.
It allows you to read line items, quantities, prices, discounts, and totals directly from Shopify’s cart.
This variable is available globally whenever a user has an active cart session. If no items have been added, cart will be empty or null.
Properties of cart
Property
Type
Scope
Description
Example
cart.cartId
String
Global
Unique Shopify GID of the current cart session.
gid://shopify/Cart/cart_id
cart.items
Object[]
Global
Array of line items currently in the cart.
See properties of cart items
cart.appliedDiscounts
Object[]
Global
Discount codes applied to the cart. Each object includes code, title, and targetType.
See properties of appliedDiscounts
cart.cartLevelDiscounts
Object[]
Global
Discounts allocated at the cart level, including the discounted amount.
See properties of cartLevelDiscounts
cart.subtotal
Float
Global
Subtotal amount before discounts/taxes.
20.99
cart.currency
String
Global
ISO currency code for prices (e.g., USD, EUR).
USD
Properties of appliedDiscounts
Property
Type
Description
Example
code
String
Discount code applied to the cart.
SAVE10
title
String
Display title of the discount. Can be null.
null
targetType
String
Where the discount applies. LINE_ITEM or CART_LEVEL.
LINE_ITEM
Properties of cartLevelDiscounts
Property
Type
Description
Example
code
String
Discount code allocated at the cart level.
SAVE10
amount
Float
Discount amount allocated at the cart level.
2.00
targetType
String
Discount allocation target. CART_LEVEL for this array.
CART_LEVEL
Properties of cart items
Property
Type
Description
Example
cart.items.lineItemId
String
Shopify GID of the line item.
gid://shopify/CartLine/line_item_id
cart.items.image
String
Image URL of the line item.
https://cdn.shopify.com/image.jpg
cart.items.compareAtAmountPerQuantity
Float
Compare-at price per quantity. Can be null.
null
cart.items.amountPerQuantity
String
Price per quantity of the line item.
10.0
cart.items.title
String
Product title.
Product title
cart.items.productId
String
Product ID.
123456789
cart.items.price
String
Line item price.
10.0
cart.items.currency
String
Currency of the line item.
USD
cart.items.quantity
Integer
Quantity of the line item.
1
cart.items.variantId
String
Variant ID.
12345
cart.items.variantName
String
Variant display name. Can be an empty string.
Small
cart.items.sellingPlanId
String
Selling plan GID when the item is a subscription. Starts with gid://shopify/SellingPlan/.
gid://shopify/SellingPlan/selling_plan_id
cart.items.lineLevelDiscounts
Object[]
Discounts allocated to this line item.
See properties of lineLevelDiscounts
cart.items.attributes
Object[]
Custom key-value attributes on the line item. Setting key as _read_only and value as true prevents quantity edits.
[{ key: "custom_key", value: "custom_value" }]
Properties of lineLevelDiscounts
Property
Type
Description
Example
code
String
Discount code applied to this line item.
BXGY
amount
Float
Discount amount allocated to this line item. Can be 0.
Check for null or empty carts: Always verify that cart exists and has items before accessing its properties.
Re-read live cart data: Inside Superfans.listeners.onCartUpdated, read Superfans.variables.cart again instead of using a cached cart object.
Use the new discount objects: Read code and targetType from appliedDiscounts. Use cartLevelDiscounts and lineLevelDiscounts for amounts. Do not treat appliedDiscounts as an array of strings.
Avoid direct modification: Cart data is read-only via this variable. Use cart actions such as addToCart and removeFromCart to change contents.
Caveats
Discount shape: appliedDiscounts is an array of objects (code, title, targetType), not an array of strings.
Split allocations: A code can appear in appliedDiscounts as LINE_ITEM and also in cartLevelDiscounts as CART_LEVEL. Line-item amounts live in items[].lineLevelDiscounts.
Zero amounts: lineLevelDiscounts.amount can be 0 on some line items even when the code is present.
Nullable fields: compareAtAmountPerQuantity and appliedDiscounts.title can be null. variantName can be an empty string.
Price rounding: Shopify handles rounding and currency precision; do not manually round floats.
Subscriptions: If using Shopify selling plans, sellingPlanId is present only on subscription line items and is a GID (gid://shopify/SellingPlan/...).