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 identifier of the current cart session.jB3892Bhj4
cart.itemsItems[]GlobalArray of line items currently in the cart.[{"quantity", "productId", "variantId", "sellingPlanId", "attributes"}]
cart.appliedDiscountsString[]GlobalArray of discount codes applied to the cart.["TEST10","GET20"]
cart.subtotalFloatGlobalSubtotal amount before discounts/taxes.20.99
cart.currencyStringGlobalISO currency code for prices (e.g., USD, EUR).USD
cart.attributesObject[]GlobalCustom attributes appended to the cart in key-value pair.[ "key": "value" ]
cart.notesStringGlobalText content available on cart as notesSample note

Properties of cart items

PropertyTypeDescriptionExample
cart.items.lineItemIdStringID of each item added to the cart.b45jhb34jh5biy234
cart.items.imageStringUrl of the images of each item added to carthttps://sampleimage.com/test.png
cart.items.compareAtAmountPerQuantityFloatthe compare at price per quantity of each item added to cart1.2
cart.items.amountPerQuantityFloatthe price per quantity of each item added to cart1.2
cart.items.titleStringName of the Product added to the cart"Shoes"
cart.items.productIdStringID of the product added to cart234245345646
cart.items.priceStringPrice of the variant"498.0"
cart.items.currencyStringCurrency of the variant"USD"
cart.items.quantityIntegerNumber of each item of the product added to the cart3
cart.items.variantIdStringID of the variant of the particular product added to cart97976974623974
cart.items.sellingPlanIdStringUnique selling plan ID of the product (if Subscription is enabled)234899822
cart.items.attributesObject[]Custom key-value pair of each item added to cart["key": "value"]
cart.items.discountsString[]Discount applied at individual item of the cart["ITEM10"]

Example Usage

const cart = VajroSDK.variables.cart;

if (cart && cart.items?.length > 0) {
  console.log(`You have ${cart.totalQuantity} items in your cart.`);

  // Show first item info
  const firstItem = cart.items[0];
  console.log(`First item: ${firstItem.title} - ${firstItem.quantity} pcs`);

  // Display subtotal and discount applied
  console.log(`Subtotal: $${cart.subtotal}`);
  console.log(`Discounts applied: $${cart.appliedDiscounts}`);
} 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. Reflect real-time changes: The cart updates automatically when items are added or removed. Use event listeners or refresh functions to re-render totals dynamically.
  3. Avoid direct modification: Cart data is read-only via this variable. Use cart actions (like addToCart, removeFromCart) to modify contents.

Caveats

  1. Price rounding: Shopify handles rounding and currency precision; do not manually round floats.
  2. Discount structures: Some discount allocations may be split per line item instead of the full cart; handle both cases.
  3. Subscriptions: If using Shopify selling plans, ensure sellingPlanAllocation exists before referencing it.