The cart drop-in component provides API functions that allow developers to retrieve and display detailed cart information dynamically.
addProductsToCart
The addProductsToCart function adds products to a cart. You must supply a sku and quantityfor each product. The other parameters are specified for complex product types. The function calls the addProductsToCart mutation.
Parameter
Type
Req?
Description
sku
string
Yes
The SKU of the product.
parentSku
string
No
For a child product, the SKU of its parent product.
quantity
number
Yes
The amount or number of an item to add.
optionsUIDs
string[]
No
The selected options for the base product, such as color or size, using the unique ID for an object.
enteredOptions
{ uid: string; value: string }[]
No
An array of entered options for the base product, such as personalization text.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Events
The event bus emits the cart/updated and cart/data events with the CartModel as the data payload. It also publishes add-to-cart or remove-from-cart events to the Adobe Client Data Layer (ACDL).
Usage
To add a simple product to the cart:
To add a configurable product to the cart:
applyCouponsToCart
The applyCouponsToCart function applies one or more predefined coupon codes to the specified cart. Any previously applied coupons are replaced. The function calls the applyCouponsToCart mutation.
Parameter
Type
Req?
Description
couponCodes
string
Yes
An array of coupon codes to apply to the cart.
type
ApplyCouponsStrategy
Yes
Indicates whether the specified coupons replace (REPLACE) or are added (APPEND) to any previously applied coupons.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Events
The event bus emits the cart/updated and cart/data events with the CartModel as the data payload.
Usage
To apply a single coupon, replacing any existing coupons in the cart:
To apply multiple coupons to the cart:
applyCouponsToCart
A function that applies or replaces one or more coupons to the cart.
Apply or replace a single coupon to the cart:
Apply or replace multiple coupons to the cart:
Here’s the shape of the data returned from the applyCouponsToCart function:
createEmptyCart
The createEmptyCart function creates an empty shopping cart for a guest or logged in customer. It returns a cart ID. The function calls the createEmptyCart mutation.
Returns
The data returned is a new empty cart ID as a string:
If guest carts have been disabled from the Admin, the function returns an error.
Usage
To create an empty cart:
getCartData
The getCartData function is mainly used internally by the initializeCart() and refreshCart() functions. If you need detailed information about the current user’s shopping cart, a more optimal approach is to listen for cart/data or cart/updated events so that you do not need to make another network call.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Usage
To get detailed information about the user’s shopping cart:
getEstimatedTotals
A function that returns estimated totals for cart based on an address. It takes an address parameter.
Parameter
Type
Req?
Description
address
EstimateAddressShippingInput
Yes
Defines the shipping address for the estimate.
The EstimateAddressShippingInput object has the following parameters:
Parameter
Type
Req?
Description
countryCode
string
Yes
The two-letter code representing the user's country.
postcode
string
No
The user's ZIP or postal code.
region
object
No
An object containing the region name, region code, and region ID.
region.region
string
No
The state or province name.
region.id
number
No
The unique ID for a pre-defined region.
shipping_method
object
No
Defines the shipping carrier and method.
shipping_method.carrier_code
string
No
A string that identifies a commercial carrier or an offline delivery method.
shipping_method.method_code
string
No
A string that indicates which service a commercial carrier will use to ship items.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Usage
To get estimated totals for the cart based on an address:
Here’s the shape of the data returned from the getEstimatedTotals function:
getEstimateShipping
The getEstimateShipping function returns a list of shipping methods and the estimated shipping costs, based on the country ID. The function calls the estimateShippingMethods mutation.
Parameter
Type
Req?
Description
address
EstimateAddressInput
No
Defines the shipping address for the estimate.
The EstimateAddressInput object has the following parameters:
Parameter
Type
Req?
Description
countryCode
string
Yes
The two-letter code representing the user's country.
postcode
string
No
The user's ZIP or postal code.
region
object
No
An object containing the region name, region code, and region ID.
region.region
string
No
The state or province name.
region.code
string
No
The address region code.
region.id
number
No
The unique ID for a pre-defined region.
Returns
Returns a promise that contains shipping data or null. The EstimatedShipping object has the following shape:
Events
The event bus emits the shipping/estimate event, which contains data about the address and shipping method.
Usage
To get an estimate of the shipping costs:
getStoreConfig
The getStoreConfig function returns information about a store’s configuration. The function calls the storeConfig query.
Returns
Returns a promise that resolves to a StoreConfigModel object or null. The StoreConfigModel object has the following shape:
Usage
To get cart-related configuration values:
initializeCart
The initializeCart function initializes a guest or customer cart. This function is automatically called during the initialize phase of a dropin’s lifecycle. You do not need to call this manually. It also emits the cart/initialized and cart/data events.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Events
The event bus emits the cart/initialized event and the cart/data event, which contains data about the address and shipping method.
Usage
To initialize a cart:
refreshCart
The refreshCart function refreshes the cart data.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Events
The event bus emits the cart/updated and cart/data events.
Usage
To refresh the cart data:
resetCart
This function resets the cart drop-in. As a result, the cart ID is set to null and the authenticated status is set to false.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Usage
To reset the cart:
updateProductsFromCart
The updateProductsFromCart function updates the quantity of a product in the cart. The specified quantity replaces the current quantity. Setting the quantity to 0 removes an item from the cart. The function calls the updateCartItems mutation.
Parameter
Type
Req?
Description
items
UpdateProductsFromCart
Yes
An input object that defines products to be updated.
The UpdateProductsFromCart object has the following parameters:
Parameter
Type
Req?
Description
uid
string
Yes
The unique ID of an item in the cart.
quantity
number
Yes
The new quantity of the item.
Returns
Returns a promise that resolves to a CartModel object or null. The CartModel object has the following shape:
Events
This function emits the cart/updated and cart/data events. It also publishes add-to-cart or remove-from-cart events to the Adobe Client Data Layer (ACDL).