Skip to main content

Add to Cart

This is the optional step 4 of the lifecycle. When a visitor clicks "Add to cart" in the Configurator, your webshop takes over the configuration and adds it to its own cart.

Cart and basket

In DIVA's events and API, a saved configuration is called a basket (e.g. onAddToBasket, GET /baskets/<divaNr>). Your backend fetches this DIVA basket and adds it to your own cart.

Prerequisites​

  • The Configurator is opened from your product pages, see Start a New Configuration.
  • A configuration page exists, see Reopen a Saved Configuration. It is also used for the "Edit" link on the cart page.
  • Your webshop stores the DIVA productId alongside each shop article, so it can start the Configurator for the right product.
  • You have received an apiKey from Crystal Design for your backend.

The flow​

  1. Your frontend receives the onAddToBasket event.
  2. Your frontend forwards the divaNr to your backend.
  3. Your backend fetches the configuration from the DIVA API, see Fetching a Configuration.
  4. Your backend converts the configuration to cart line items and adds them to the cart, see The Configuration Object.
warning

Always fetch the configuration in your backend with the apiKey. Do not trust prices or articles sent by the browser, and never expose the apiKey in your frontend code.

Listen for "Add to cart"​

Name a function for the onAddToBasket event in data-diva-event-onaddtobasket. The function receives the Configuration Object, including the DivaNr.

Set the attribute on every widget element that opens the Configurator, including the one on your configuration page, so that "Add to cart" also works after reopening a configuration.

<div
data-diva-widget-type="diva-configurator-button"
data-diva-param-productid="<PRODUCT_ID>"
data-diva-event-onaddtobasket="addToCart"
>
Configure product
</div>

<script>
async function addToCart(detail) {
const { DivaNr, DivaNrVersion } = detail;

// Forward the divaNr to your backend, which fetches the configuration from the DIVA API
await fetch('/api/cart/add-diva-configuration', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ divaNr: DivaNrVersion || DivaNr }),
});

window.location.href = '/cart';
}
</script>

<script src="https://<your-diva-api-host>/integration/api/widget/<YOUR_ORGANIZATION_ID>"></script>

The endpoint /api/cart/add-diva-configuration is an example and stands for your own backend.

Cart page​

On the cart page, provide two actions for each DIVA configuration:

  • Edit configuration: a link to your configuration page with the saved divaNr, e.g. /configurator?divaNr=DIVA-12345-1
  • DIVA PDF: a link to the PDF document, taken from the PDFDocumentURL of the Configuration Object

Reusing the configuration page for "Edit" means there is only one place in your website that reopens configurations.