Skip to content

Search is only available in production builds. Try building and previewing the site to test it out locally.

Cart initialization

The Cart initializer configures how the cart manages shopping cart data, including items, pricing, discounts, and customer information. Use initialization to customize cart behavior, enable guest cart features, and transform cart data models to match your storefront requirements.

Version: 3.1.0

The following table describes the configuration options available for the Cart initializer:

ParameterTypeReq?Description
langDefinitionsLangDefinitionsNoLanguage definitions for internationalization (i18n). Override dictionary keys for localization or branding.
modelsRecord<string, any>NoCustom data models for type transformations. Extend or modify default models with custom fields and transformers.
disableGuestCartbooleanNoWhen set to `true`, prevents guest users from creating or accessing shopping carts, requiring authentication before cart operations.

The initializer runs with these defaults when no configuration is provided:

import { initializers } from '@dropins/tools/initializer.js';
import { initialize } from '@dropins/storefront-cart';
// All configuration options are optional
await initializers.mountImmediately(initialize, {
langDefinitions: {}, // Uses built-in English strings
models: {}, // Uses default data models
// Drop-in-specific defaults:
// disableGuestCart: undefined // See configuration options below
});

Override dictionary keys for localization or branding. The langDefinitions object maps locale keys to custom strings that override default text for the drop-in.

import { initializers } from '@dropins/tools/initializer.js';
import { initialize } from '@dropins/storefront-cart';
const customStrings = {
'AddToCart': 'Add to Bag',
'Checkout': 'Complete Purchase',
'Price': 'Cost',
};
const langDefinitions = {
default: customStrings,
};
await initializers.mountImmediately(initialize, { langDefinitions });

Extend or transform data models by providing custom transformer functions. Use the models option to add custom fields or modify existing data structures returned from the backend.

The following models can be customized through the models configuration option:

ModelDescription
CartModelTransforms cart data from GraphQL including items, totals, discounts, taxes, gift options, addresses, and payment methods. Use this to add custom fields or modify existing cart data structures.

The following example shows how to customize the CartModel model for the Cart drop-in:

import { initializers } from '@dropins/tools/initializer.js';
import { initialize } from '@dropins/storefront-cart';
const models = {
CartModel: {
transformer: (data) => ({
// Add custom fields from backend data
customField: data?.custom_field,
promotionBadge: data?.promotion?.label,
// Transform existing fields
displayPrice: data?.price?.value ? `${data.price.value}` : 'N/A',
}),
},
};
await initializers.mountImmediately(initialize, { models });

The Cart initializer configures how the cart manages shopping cart data, including items, pricing, discounts, and customer information. Use initialization to customize cart behavior, enable guest cart features, and transform cart data models to match your storefront requirements.

import { initializers } from '@dropins/tools/initializer.js';
import { initialize } from '@dropins/storefront-cart';
await initializers.mountImmediately(initialize, {
disableGuestCart: true,
langDefinitions: {},
models: {},
});

The following TypeScript definitions show the structure of each configuration object:

Maps locale identifiers to dictionaries of key-value pairs. The default locale is used as the fallback when no specific locale matches. Each dictionary key corresponds to a text string used in the drop-in UI.

langDefinitions?: {
[locale: string]: {
[key: string]: string;
};
};

Maps model names to transformer functions. Each transformer receives data from GraphQL and returns a modified or extended version. Use the Model<T> type from @dropins/tools to create type-safe transformers.

models?: {
[modelName: string]: Model<any>;
};

The following TypeScript definitions show the structure of each customizable model:

export interface CartModel {
totalGiftOptions: {
giftWrappingForItems: Price;
giftWrappingForItemsInclTax: Price;
giftWrappingForOrder: Price;
giftWrappingForOrderInclTax: Price;
printedCard: Price;
printedCardInclTax: Price;
};
cartGiftWrapping: {
uid: string;
design: string;
selected: boolean;
image: WrappingImage;
price: Price;
}[];
giftReceiptIncluded: boolean;
printedCardIncluded: boolean;
giftMessage: {
recipientName: string;
senderName: string;
message: string;
};
appliedGiftCards: AppliedGiftCardProps[];
id: string;
totalQuantity: number;
totalUniqueItems: number;
errors?: ItemError[];
items: Item[];
miniCartMaxItems: Item[];
total: {
includingTax: Price;
excludingTax: Price;
};
discount?: Price;
subtotal: {
excludingTax: Price;
includingTax: Price;
includingDiscountOnly: Price;
};
appliedTaxes: TotalPriceModifier[];
totalTax?: Price;
appliedDiscounts: TotalPriceModifier[];
shipping?: Price;
isVirtual?: boolean;
addresses: {
shipping?: {
countryCode: string;
zipCode?: string;
regionCode?: string;
}[];
};
isGuestCart?: boolean;
hasOutOfStockItems?: boolean;
hasFullyOutOfStockItems?: boolean;
appliedCoupons?: Coupon[];
}