# Cart Object

Cart object is meant to provide advanced control while using the drop-in script to dynamically change customer details and product information with the help of the product object. It contains customer related information such as shipping, billing information, and product information.

Use getCart to retrieve the cart object.

# replaceProduct

Use this method to add plan product to your cart. Use cbInstance.initializeProduct or cbInstance.getProduct to get the product object.

# Syntax

cart.replaceProduct(product);
1

# Parameters

product
Product object created using cbInstance.initializeProduct or cbInstance.getProduct.

# Return value

Returns the Cart object for function chaining.

# Example

function onClickSubscribe(planId) {
    const cbInstance = Chargebee.getInstance();
    const cart = cbInstance.getCart();
    const product = cbInstance.initializeProduct(planId);
    cart.replaceProduct(product);
    cart.proceedToCheckout();
}
1
2
3
4
5
6
7

# proceedToCheckout

This method helps to open the checkout.

# Syntax

cart.proceedToCheckout();
1

# Return value

NA

# Example

function onClickSubscribe(planId) {
    const cbInstance = Chargebee.getInstance();
    const cart = cbInstance.getCart();
    const product = cbInstance.initializeProduct(planId);
    cart.replaceProduct(product);
    cart.proceedToCheckout();
}
1
2
3
4
5
6
7

# setBusinessEntity

Sets the business entity context for checkout.

# Syntax

cart.setBusinessEntity(businessEntityId)
1

# Parameters

entityId
String Required
Id of business entity.

# Return value

This function returns a promise.

# setAffiliateToken

Sets subscription affiliate token (opens new window) during checkout.

# Syntax

cart.setAffiliateToken(token)
1

# Parameters

token
String
Affiliate token to be recorded against the subscription.

# Return value

Cart object.

# setCustomer

Use this method to pre-fill customer information, billing address in Checkout, or to pass any hidden fields while creating a subscription. This function also supports customer custom fields.

# Syntax

cart.setCustomer(customer)

1
2

# Parameters

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

Cart object.

# Example

const cbInstance = Chargebee.getInstance();
const cart = cbInstance.getCart();
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"
    }
};

// Setting custom fields
customer["cf_about_myself"] = "This is short description"
cart.setCustomer(customer);
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

# setShippingAddress

Use this method to pre-fill shipping address information.

# Syntax

cart.setShippingAddress(shippingAddress)

1
2

# Parameters

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

Cart object.

# Example

const cbInstance = Chargebee.getInstance();
const cart = cbInstance.getCart();
const shippingAddress = {
    first_name: "John", 
    last_name: " Smith", 
    line1: "132, My Street", 
    line2: "Kingston, New York",
    state_code: "NY",
    country: "US",
    zip: "12401"
}
cart.setShippingAddress(shippingAddress);
1
2
3
4
5
6
7
8
9
10
11
12