Cart

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

PropertyTypeScopeDescriptionExample
cart.cartIdStringGlobalUnique Shopify GID of the current cart session.gid://shopify/Cart/cart_id
cart.itemsObject[]GlobalArray of line items currently in the cart.See properties of cart items
cart.appliedDiscountsObject[]GlobalDiscount codes applied to the cart. Each object includes code, title, and targetType.See properties of appliedDiscounts
cart.cartLevelDiscountsObject[]GlobalDiscounts allocated at the cart level, including the discounted amount.See properties of cartLevelDiscounts
cart.subtotalFloatGlobalSubtotal amount before discounts/taxes.20.99
cart.currencyStringGlobalISO currency code for prices (e.g., USD, EUR).USD

Properties of appliedDiscounts

PropertyTypeDescriptionExample
codeStringDiscount code applied to the cart.SAVE10
titleStringDisplay title of the discount. Can be null.null
targetTypeStringWhere the discount applies. LINE_ITEM or CART_LEVEL.LINE_ITEM

Properties of cartLevelDiscounts

PropertyTypeDescriptionExample
codeStringDiscount code allocated at the cart level.SAVE10
amountFloatDiscount amount allocated at the cart level.2.00
targetTypeStringDiscount allocation target. CART_LEVEL for this array.CART_LEVEL

Properties of cart items

PropertyTypeDescriptionExample
cart.items.lineItemIdStringShopify GID of the line item.gid://shopify/CartLine/line_item_id
cart.items.imageStringImage URL of the line item.https://cdn.shopify.com/image.jpg
cart.items.compareAtAmountPerQuantityFloatCompare-at price per quantity. Can be null.null
cart.items.amountPerQuantityStringPrice per quantity of the line item.10.0
cart.items.titleStringProduct title.Product title
cart.items.productIdStringProduct ID.123456789
cart.items.priceStringLine item price.10.0
cart.items.currencyStringCurrency of the line item.USD
cart.items.quantityIntegerQuantity of the line item.1
cart.items.variantIdStringVariant ID.12345
cart.items.variantNameStringVariant display name. Can be an empty string.Small
cart.items.sellingPlanIdStringSelling plan GID when the item is a subscription. Starts with gid://shopify/SellingPlan/.gid://shopify/SellingPlan/selling_plan_id
cart.items.lineLevelDiscountsObject[]Discounts allocated to this line item.See properties of lineLevelDiscounts
cart.items.attributesObject[]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

PropertyTypeDescriptionExample
codeStringDiscount code applied to this line item.BXGY
amountFloatDiscount amount allocated to this line item. Can be 0.10

Sample cart object

{"cartId":"gid://shopify/Cart/cart_id","appliedDiscounts":[{"code":"SAVE10","title":null,"targetType":"LINE_ITEM"},{"code":"BXGY","title":null,"targetType":"LINE_ITEM"}],"cartLevelDiscounts":[{"code":"SAVE10","amount":2.00,"targetType":"CART_LEVEL"}],"subtotal":20.99,"currency":"USD","items":[{"variantId":"12345","productId":"123456789","quantity":1,"lineItemId":"gid://shopify/CartLine/line_item_id","title":"Product title","price":"10.0","currency":"USD","compareAtAmountPerQuantity":null,"image":"https://cdn.shopify.com/image.jpg","amountPerQuantity":"10.0","variantName":"Small","sellingPlanId":"gid://shopify/SellingPlan/selling_plan_id","lineLevelDiscounts":[{"code":"BXGY","amount":10}],"attributes":[{"key":"custom_key","value":"custom_value"}]}]}}

Example Usage

const cart = Superfans.variables.cart;

if (cart && cart.items?.length > 0) {
  const firstItem = cart.items[0];
  const appliedCodes = (cart.appliedDiscounts || []).map((discount) => discount.code);
  const cartDiscountTotal = (cart.cartLevelDiscounts || []).reduce((sum, discount) => {
    return sum + (discount.amount || 0);
  }, 0);
  const lineDiscountTotal = (firstItem.lineLevelDiscounts || []).reduce((sum, discount) => {
    return sum + (discount.amount || 0);
  }, 0);

  console.log(`Cart ID: ${cart.cartId}`);
  console.log(`Items: ${cart.items.length}`);
  console.log(`First item: ${firstItem.title} - ${firstItem.variantName}`);
  console.log(`Subtotal: ${cart.subtotal} ${cart.currency}`);
  console.log(`Applied codes: ${appliedCodes.join(", ")}`);
  console.log(`Cart-level discount: ${cartDiscountTotal}`);
  console.log(`First item line-level discount: ${lineDiscountTotal}`);
  if (firstItem.sellingPlanId) {
    console.log(`Selling plan GID: ${firstItem.sellingPlanId}`);
  }
} else {
  console.log("Your cart is empty.");
}

Best Practices

  1. Check for null or empty carts: Always verify that cart exists and has items before accessing its properties.
  2. Re-read live cart data: Inside Superfans.listeners.onCartUpdated, read Superfans.variables.cart again instead of using a cached cart object.
  3. 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.
  4. Avoid direct modification: Cart data is read-only via this variable. Use cart actions such as addToCart and removeFromCart to change contents.

Caveats

  1. Discount shape: appliedDiscounts is an array of objects (code, title, targetType), not an array of strings.
  2. 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.
  3. Zero amounts: lineLevelDiscounts.amount can be 0 on some line items even when the code is present.
  4. Nullable fields: compareAtAmountPerQuantity and appliedDiscounts.title can be null. variantName can be an empty string.
  5. Price rounding: Shopify handles rounding and currency precision; do not manually round floats.
  6. Subscriptions: If using Shopify selling plans, sellingPlanId is present only on subscription line items and is a GID (gid://shopify/SellingPlan/...).

Did this page help you?