# 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:
- Initialized Chargebee.js in your application.
- Created a product object to add to the cart.
# Create the Cart Object
To retrieve the Cart object, use the getCart() method:
const chargebee = Chargebee.getInstance();
const cart = chargebee.getCart();
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)
# Parameters
DETAILS
# 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);
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)
# Parameters
# 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();
2
3
4
5
6
7
8
9
10
# setShippingAddress(shippingAddress)
Pre-fills shipping address information in the checkout form.
# Syntax
cart.setShippingAddress(shippingAddress)
# Parameters
DETAILS
# 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);
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);
# Parameters
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();
}
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)
# Parameters
# 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();
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();
# 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();
}
2
3
4
5
6
7
8
9
10
11