Cart Object

Overview

The Cart object provides advanced control over the checkout process, allowing you to dynamically manage customer details, product information, and billing data before proceeding to checkout.

Tutorials

For a step-by-step integration, see Drop-in Checkout with Chargebee.js.

Prerequisites

Before using the Cart object, ensure you have:

Create the Cart Object

To retrieve the Cart object, use the getCart() method:

const chargebee = Chargebee.getInstance();
const cart = chargebee.getCart();

setCustomer(customer)

Pre-fills customer information and billing address in the checkout form, or passes hidden fields when creating a subscription. You can also use this method to set customer-level custom fields.

Syntax

cart.setCustomer(customer)

Parameters

Return value

Returns the Cart object for method chaining, allowing you to call additional cart methods.

Example

const chargebee = Chargebee.getInstance();
const cart = chargebee.getCart();

// Create customer object with basic information.
const customer = {
    first_name: "John",
    last_name: "Smith",
    locale: "it",
    billing_address: {
        first_name: "John",
        last_name: "Smith",
        line1: "132, My Street",
        line2: "Kingston, New York",
        state_code: "NY",
        country: "US",
        zip: "12401"
    }
};

// Add custom fields.
customer["cf_about_myself"] = "This is a short bio.";

// Set customer information in cart.
cart.setCustomer(customer);

setBusinessEntity(businessEntityId)

Sets the business entity to use for creating or updating customer and subscription records in Chargebee Billing. If a business entity was set when creating the Chargebee object, this method overrides it.

Syntax

cart.setBusinessEntity(businessEntityId)

Parameters

entityId
required, String

Return value

This function returns a promise that resolves when the business entity is set.

Example

const chargebee = Chargebee.getInstance();
const cart = chargebee.getCart();

// Set business entity for checkout.
cart.setBusinessEntity("acme-inc-us");

// Add product and proceed to checkout.
const product = chargebee.initializeProduct("premium-monthly");
cart.replaceProduct(product);
cart.proceedToCheckout();

setShippingAddress(shippingAddress)

Pre-fills shipping address information in the checkout form.

Syntax

cart.setShippingAddress(shippingAddress)

Parameters

Return value

Returns the Cart object for method chaining, allowing you to call additional cart methods.

Example

const chargebee = Chargebee.getInstance();
const cart = chargebee.getCart();

// Create shipping address object.
const shippingAddress = {
    first_name: "John",
    last_name: "Smith",
    line1: "132, My Street",
    line2: "Kingston, New York",
    state_code: "NY",
    country: "US",
    zip: "12401"
};

// Set shipping address in cart.
cart.setShippingAddress(shippingAddress);

replaceProduct(product)

Adds a product to your cart, replacing any existing products. Use this method to set up the items that will be included in the checkout process.

Syntax

cart.replaceProduct(product);

Parameters

product

Return value

Returns the Cart object for function chaining, allowing you to call additional cart methods.

Example

function onClickSubscribe(planId) {
    const chargebee = Chargebee.getInstance();
    const cart = chargebee.getCart();
    
    // Create a product object.
    const product = chargebee.initializeProduct(planId);
    
    // Add the product to the cart.
    cart.replaceProduct(product);
    
    // Proceed to checkout.
    cart.proceedToCheckout();
}

setAffiliateToken(token)

Sets a subscription affiliate token for tracking affiliate referrals during checkout.

Syntax

cart.setAffiliateToken(token)

Parameters

token
String

Return value

Returns the Cart object for function chaining, allowing you to call additional cart methods.

Example

const chargebee = Chargebee.getInstance();
const cart = chargebee.getCart();

// Set affiliate token for tracking.
cart.setAffiliateToken("affiliate_12345");

// Add product and proceed to checkout.
const product = chargebee.initializeProduct("premium-monthly");
cart.replaceProduct(product);
cart.proceedToCheckout();

proceedToCheckout()

Opens the Chargebee Checkout page with the items and customer information currently in the cart. This method initiates the complete checkout flow, including payment processing.

Syntax

cart.proceedToCheckout();

Return value

This function does not return a value.

Example

function onClickSubscribe(planId) {
    const chargebee = Chargebee.getInstance();
    const cart = chargebee.getCart();
    
    // Create and add product to cart.
    const product = chargebee.initializeProduct(planId);
    cart.replaceProduct(product);
    
    // Open checkout.
    cart.proceedToCheckout();
}