Add to Cart

Adds an item to cart

Description

The addToCart() action adds a specific product or variant to the user’s active shopping cart inside the app.
It works seamlessly with the native Superfans cart and triggers a cartUpdated event once the item is added successfully.
You can use this action from any custom block, PDP extension, or embedded web view to provide a smooth “Add to Cart” experience without reloading the page.

Function Signature

Superfans.actions.addToCart([
    {
      variantId: "12345",
      quantity: 1,
      sellingPlanId: "gid://shopify/SellingPlan/selling_plan_id", // Optional. Must be a GID.
      attributes: [{ "key":"custom_key","value":"custom_value"}] // Optional
    },
  ])
ParameterTypeRequiredDescription
variantIdStringYesThe unique ID of the variant of the product
quantityNumberYesThe number of items of product added to cart
sellingPlanIdStringNoShopify selling plan GID from variant.sellingPlanAllocations[].sellingPlanId. Must start with gid://shopify/SellingPlan/. Do not pass a numeric id.
attributesObjectNoCustom Key Value Pairs to pass any additional info about the product

Structure of Response

StatusResponse
Success{ "status": "success", "message": "Line items added to cart" }
Invalid Params{ "status": "error", "errorId": 400, "errorHandle": "invalid-params", "message": "Invalid params" }
Unexpected Error{ "status": "error", "errorId": 500, "errorHandle": "unknown-error", "message": "Something unexpected happened" }

Example Usage

const product_data = Superfans.variables.product;
const variant = product_data?.selectedVariant || product_data?.variants?.find((item) => item.availableForSale) || product_data?.variants?.[0];
const variant_id = variant?.id;
const quantity = 1;
const sellingPlanId = variant?.sellingPlanAllocations?.[0]?.sellingPlanId;
const prod_metafield = product_data.metafields["product_height"];
const customAttributes = [
  { key: "product_height", value: prod_metafield },
  { key: "delivery_info", value: "Est. delivery 5 working days" }
];
function addItemToCartWithAttributes(variantId, quantity, customAttributes, sellingPlanId) {
  const line = {
    variantId: variantId,
    quantity: quantity,
    attributes: customAttributes
  };
  if (sellingPlanId) {
    line.sellingPlanId = sellingPlanId;
  }
  Superfans.actions.addToCart([line])
    .then(() => {
      console.log("Item added to cart with attributes");
    })
    .catch(err => {
      console.error("Error adding item to cart:", err);
    });
}
function addToCart()
{
addItemToCartWithAttributes(variant_id, quantity, customAttributes, sellingPlanId);
}

Best Practices

  1. Use variantId when products have multiple options like size or color.
  2. Provide feedback to the user with showToast() or showAlert().
  3. Combine with the cartUpdated listener to refresh cart UI or item counts dynamically.
  4. Validate stock availability before adding to the cart (if using custom logic).
  5. For subscriptions, copy sellingPlanId from sellingPlanAllocations. Keep the gid:// prefix.

Caveats

  1. If the product requires variant selection, variantId must be specified.
  2. Does not open the cart automatically — use Superfans.actions.openCart() if needed.
  3. Adding out-of-stock items may fail silently; handle this with proper validation.
  4. The cartUpdated event is triggered asynchronously after the update.
  5. sellingPlanId must be a Shopify GID (gid://shopify/SellingPlan/...). A numeric plan id is not valid.

Did this page help you?