# 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.

# 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();
1
2

# 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)
1

# Parameters

DETAILS
customer
Object Hide properties
Customer details.
first_name
String
Customer first name.
last_name
String
Customer last name.
locale
String
Locale code in ISO 639-1 format (en, fr). By default, `en` will be used. Learn more about supported locales by Chargebee.
company
String
Company name.
phone
String
Customer phone number.
email
String
Customer mail ID.
billingAddress
Object Hide properties
first_name
String
First name
last_name
String
Last name
company
String
Company name
phone
String
Phone number.
email
String
Email ID
line1
String
Address line 1
line2
String
Address line 2
city
String
City
state
String
State name
state_code
String
State code
country
String
Country code
zip
String
Zip/Postal code

# 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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24

# setBusinessEntity(businessEntityId)

Sets the business entity (opens new window) 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)
1

# Parameters

entityId
String Required
Id of business entity.

# 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();
1
2
3
4
5
6
7
8
9
10

# setShippingAddress(shippingAddress)

Pre-fills shipping address information in the checkout form.

# Syntax

cart.setShippingAddress(shippingAddress)
1

# Parameters

DETAILS
shippingAddress
Object Hide properties
first_name
String
First name
last_name
String
Last name
company
String
Company name
phone
String
Phone number.
email
String
Email ID
line1
String
Address line 1
line2
String
Address line 2
city
String
City
state
String
State name
state_code
String
State code
country
String
Country code
zip
String
Zip/Postal code

# 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);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# 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);
1

# Parameters

product
Product object created using initializeProduct() or getProduct().

# 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();
}
1
2
3
4
5
6
7
8
9
10
11
12
13

# setAffiliateToken(token)

Sets a subscription affiliate token (opens new window) for tracking affiliate referrals during checkout.

# Syntax

cart.setAffiliateToken(token)
1

# Parameters

token
String
Affiliate token to be recorded against the subscription.

# 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();
1
2
3
4
5
6
7
8
9
10

# 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();
1

# 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();
}
1
2
3
4
5
6
7
8
9
10
11