Add to Cart
Add to Cart
Carousels with enableAddToCart and the welcome screen "Buy it again" card show an Add to cart button. The widget runs on your storefront's domain, so it adds products with your store's own cart request and the shopper's session.
The widget uses the first of these that applies, and hides the button if none do:
callbacks.onAddToCart: a callback on your page that adds the product itself.- Shopify: detected automatically on Online Store themes. Nothing to configure.
- A supported platform:
ecomPlatform(sfcc,magento,woocommerceorbigcommerce) and, if needed,cartin the server-side widget config (Widget Builder → Advanced). No code changes on your site.
Your DigitalGenius Solutions Engineer can build the configured request from an add to cart request recorded on your store.
Shopify
Nothing to configure on Online Store themes: the widget detects Shopify from the page (e.g. window.Shopify, or scripts from cdn.shopify.com) and adds with Shopify's AJAX API, POST /cart/add.js with the product id and quantity. If detection doesn't work on your theme, set it explicitly:
{
"ecomPlatform": "shopify"
}Carousel product ids must be variant IDs. cart and cartParams aren't used. Headless storefronts (e.g. Hydrogen) need the callback.
The widget adds with /cart/add.js, so a theme whose cart drawer or count only redraws after its own add to cart shows the old cart until the next page load. Call the theme's cart refresh from the onItemAddedToCart callback.
Salesforce Commerce Cloud (SFRA)
{
"ecomPlatform": "sfcc",
"cart": {
"add": {
"url": "/on/demandware.store/Sites-Example-Site/en_GB/Cart-AddProduct"
}
}
}The URL contains your site ID and locale, so each regional site needs its own widget config. Carousel product ids must be variant product IDs.
Magento 2 / Adobe Commerce
{
"ecomPlatform": "magento"
}The widget posts to /checkout/cart/add with product, qty and the form_key cookie. Carousel slide ids must be the parent product ID; configurable options (e.g. super_attribute[93]) are sent as cartParams on the variant or size. If your store uses a store code in the URL, set cart.add.url, e.g. "/en/checkout/cart/add".
WooCommerce
{
"ecomPlatform": "woocommerce",
"cart": {
"add": {
"tokens": { "nonce": { "global": "path.to.nonce" } }
}
}
}The widget posts to the Store API (/wp-json/wc/store/v1/cart/add-item) with id, quantity and the Nonce header. Where the page exposes the nonce varies by theme, so tokens.nonce must say where to read it (see add.tokens below); your Solutions Engineer can find it. If WordPress is in a subdirectory, set cart.add.url, e.g. "/shop/wp-json/wc/store/v1/cart/add-item". Carousel product ids must be the product or variation ID.
BigCommerce (Stencil themes)
{
"ecomPlatform": "bigcommerce"
}The widget adds to the cart the way Stencil themes do: it posts the product form to /remote/v1/cart/add with product_id and qty[], and the CSRF token from window.BCData.csrf_token. Carousel slide ids must be the product ID; option selections (attribute[<option id>] set to the option value ID) are sent as cartParams on the variant or size. Headless BigCommerce storefronts (e.g. Catalyst) need the callback.
Other platforms
Use the onAddToCart callback.
Options
Each platform has defaults for everything except what's specific to your store. cart is merged over them: headers, body and tokens key by key, the rest replaced.
| Property | Description |
|---|---|
add.url | Must be on the same origin as the page and the platform's add to cart path (.../Cart-AddProduct, /checkout/cart/add, .../wp-json/wc/store/v1/cart/add-item, /remote/v1/cart/add); otherwise nothing is sent. A relative path is recommended. Required for sfcc. |
add.method | Always POST. Other methods are rejected. A response that redirects counts as a failure. |
add.encoding | json, form or multipart. Each platform sets the one it needs. |
add.headers, add.body | The request headers and body. Values can use placeholders. |
add.tokens | Values read from the page on click, e.g. CSRF tokens: { "cookie": "name" }, { "meta": "name" }, { "selector": "css", "attribute": "optional" }, { "global": "path.on.window" } or { "localStorage": "key" }. If one can't be found, nothing is sent. |
add.failWhen | Rules that mark a 2xx JSON response as failed: a JSON Pointer path, plus an optional equals (otherwise any truthy value matches). |
afterAdd | Lets your theme refresh its mini cart: { "type": "event", "name": "...", "detail": "{{response}}" } dispatches a DOM event; { "type": "jquery", "name": "...", "selector": "...", "data": "{{response}}" } triggers a jQuery event on the elements matching selector, a CSS selector (default body). Names of native actions such as click or submit are refused. |
Placeholders: {{id}} (the selected product, variant or size), {{productId}} (the parent product), {{quantity}}, {{token.<name>}}, {{item.<field>}} and, in afterAdd, {{response}}.
Any cartParams the flow sends on a carousel slide, variant or size are added to the request body. They can't replace a field set in add.body. See the Carousel documentation.
Updated about 9 hours ago
