Cart Widget
The Cart Widget shows visitors where to buy a list of products, with quantities, for example the products they have selected on your website. It opens from a button whose data-* attributes describe the cart.
Technical requirements
Check the technical requirements first, especially if your website uses a Content Security Policy.
Display modes
The widget can open in 2 ways:
Clicking the button opens the widget in a window over your page.
Clicking the button opens the widget inside your page, in a block you choose.
The cart is described by the attributes of the button. Any clickable element works, such as a link or an image.
Modal
Step 1: load the script
Add our script to the pages that display the widget, or to the template used by all your pages:
html
<script type="module" src="https://widget.clic2buy.com/c2b-cart-widget.js"></script>INFO
This is not the same script as the c2b.js script of the Standard and MultiProduct widgets. If a page displays both kinds of widgets, load both scripts.
Step 2: add the attributes to your button
html
<button
data-cart-widget-id="CART_WIDGET_ID"
data-cart-widget-locale="fr-FR"
data-cart-widget-products='[{"reference":"ref-001","quantity":2},{"reference":"ref-002","quantity":1}]'
>
See product availability
</button>Inline
Step 1: load the script
Add our script to the pages that display the widget, or to the template used by all your pages:
html
<script type="module" src="https://widget.clic2buy.com/c2b-cart-widget.js"></script>INFO
This is not the same script as the c2b.js script of the Standard and MultiProduct widgets. If a page displays both kinds of widgets, load both scripts.
Step 2: add the attributes to your button
Set data-cart-widget-display-mode to inline, and data-cart-widget-inline-container to the CSS selector of the block where the widget should open:
html
<button
data-cart-widget-id="CART_WIDGET_ID"
data-cart-widget-locale="fr-FR"
data-cart-widget-products='[{"reference":"ref-001","quantity":2},{"reference":"ref-002","quantity":1}]'
data-cart-widget-display-mode="inline"
data-cart-widget-inline-container="#c2b-cart-widget"
>
See product availability
</button>Step 3: add the container
Add the block where the widget should open. It must be on the page when the visitor clicks the button:
html
<div id="c2b-cart-widget"></div>Attributes
| Attribute | Required | Description |
|---|---|---|
data-cart-widget-id | Yes | Cart Widget key, provided by your Customer Success Manager |
data-cart-widget-locale | Yes | Language and country of the widget, for example fr-FR or en-GB |
data-cart-widget-products | Yes | Products and quantities, as a JSON array: see Products format |
data-cart-widget-display-mode | No | modal (default) or inline |
data-cart-widget-inline-container | In inline mode | CSS selector of the block where the widget opens, for example #c2b-cart-widget |
Products format
data-cart-widget-products must be a non-empty JSON array, where each product has:
- exactly one identifier:
reference,eanorupc - a
quantity, as a number (2, not"2")
All products must use the same type of identifier, and no other keys are allowed.
json
[{"reference":"ref-001","quantity":2},{"reference":"ref-002","quantity":1}]json
[{"ean":"3017620425035","quantity":1},{"ean":"3017620425042","quantity":3}]json
[{"upc":"012345678905","quantity":1},{"upc":"012345678912","quantity":2}]TIP
Put the attribute value between single quotes ('…'): the JSON contains double quotes, which would otherwise end the attribute.
Update the cart
The widget reads the attributes each time the visitor clicks the button. When the visitor's selection changes, just update data-cart-widget-products: the next click opens the widget with the new products.
js
const button = document.querySelector('[data-cart-widget-id]')
button.setAttribute('data-cart-widget-products', JSON.stringify([
{ reference: 'ref-001', quantity: 3 }
]))Buttons added after the page has loaded (for example by a JavaScript framework such as React or Vue) are detected automatically: you don't need to initialize anything.
Widget design
The widget is hosted by Click2Buy, and we configure its design (shapes, colors, fonts, etc.). To match it to your brand guidelines, contact your Customer Success Manager.
Tracking
Click2Buy tracks widget usage automatically: statistics are available in our back office (ask your Customer Success Manager for more details).
Troubleshooting
If the widget doesn't open when you click the button, open the browser console and look for an error starting with [c2b-cart-widget]:
| Error | Fix |
|---|---|
Invalid products attribute | Check data-cart-widget-products against the Products format: valid JSON, one type of identifier, numeric quantities, no other keys. |
Invalid locale attribute | Add a data-cart-widget-locale attribute to the button. |
Inline container selector not found | Add a data-cart-widget-inline-container attribute to the button. |
Inline container not found for selector "…" | Check that an element matching the selector is on the page when the visitor clicks the button. |
Still stuck? Contact us or your Customer Success Manager.