Skip to main content

Show live specifications

In this tutorial you show the specifications of the configured product: a list of the selected options, per product, that updates while the shopper configures. Shoppers see what they are about to order, and you can reuse the same data for an order summary or a cart line.

The finished code is the specifications.html page of the example apps.

Where the data comes from​

The configurator keeps the products and their attributes (the options) in its store. Two functions give you access to them:

  • useStoreProductOffers(configurator): the products. For a regular product that is one product; for a bundle, the main product plus every product the shopper added.
  • useStoreAttributes(configurator): the attributes of all products. Each attribute has a name, the productCid of its product, a hidden flag, and its selected values in configuredValue.

Both have a data property with the current state and a subscribe(callback) method that calls callback after every change.

1. Build the summary​

This function is the same in all three examples. It groups the visible attributes by product and turns their selected values into text. Each selected value has a value (the label shown to the shopper, or the entered number or text), a description and an identifier.

src/shared/summary.js
import { useStoreAttributes, useStoreProductOffers } from '@expivi/sdk';

/**
* Builds a summary of the current configuration: for each product (the main product and, for
* bundles, every added product) the visible attributes with their selected values.
*
* @returns {{ productCid: string, productName: string, rows: { cid: string, label: string, value: string }[] }[]}
*/
export function getConfigurationSummary(configurator) {
const products = useStoreProductOffers(configurator).data;
const attributes = useStoreAttributes(configurator).data.filter((attribute) => !attribute.hidden);

return products
.map((product) => ({
productCid: product.cid,
productName: product.name,
rows: attributes
.filter((attribute) => attribute.productCid === product.cid)
.map((attribute) => ({
cid: attribute.cid,
label: attribute.name,
value: attribute.configuredValue.map(formatConfiguredValue).join(', ') || '—',
})),
}))
.filter((product) => product.rows.length > 0);
}

/**
* Calls `callback` with a fresh summary now and whenever products or attribute values change.
* Returns a function that stops listening.
*/
export function subscribeConfigurationSummary(configurator, callback) {
const update = () => callback(getConfigurationSummary(configurator));
const unsubscribeAttributes = useStoreAttributes(configurator).subscribe(update);
const unsubscribeProducts = useStoreProductOffers(configurator).subscribe(update);

update();

return () => {
unsubscribeAttributes();
unsubscribeProducts();
};
}

function formatConfiguredValue(configuredValue) {
const { value } = configuredValue;

if (typeof value === 'string' || typeof value === 'number') return String(value);
if (typeof value === 'boolean') return value ? 'Yes' : 'No';

return configuredValue.description || configuredValue.identifier;
}

2. Render the summary​

Render the summary once the configurator is READY, and update it from subscribeConfigurationSummary().

src/components/configuration-summary.js
import { escapeHtml } from '../shared/escape-html.js';
import { subscribeConfigurationSummary } from '../shared/summary.js';

/**
* Lists the selected options per product and updates the list while the shopper configures.
*
* @param {HTMLElement} container
* @param {import('@expivi/sdk').ProductConfigurator} configurator
* @returns {() => void} stops updating
*/
export function mountConfigurationSummary(container, configurator) {
return subscribeConfigurationSummary(configurator, (summary) => {
container.innerHTML = summary
.map(
(product) => `
<section class="summary__product">
<h3 class="summary__product-name">${escapeHtml(product.productName)}</h3>
<dl class="summary__list">
${product.rows
.map(
(row) => `
<div class="summary__row">
<dt>${escapeHtml(row.label)}</dt>
<dd>${escapeHtml(row.value)}</dd>
</div>`,
)
.join('')}
</dl>
</section>`,
)
.join('');
});
}
src/pages/specifications.js
import '../shared/setup.js';

import { mountConfigurationSummary } from '../components/configuration-summary.js';
import { mountProductConfigurator, startConfigurator } from '../shared/configurator.js';
import { renderShopHeader } from '../shared/navigation.js';

renderShopHeader('specifications');

startConfigurator({
onReady(configurator) {
mountProductConfigurator(configurator);
mountConfigurationSummary(document.querySelector('#summary'), configurator);
},
});

Result​

The specifications list every visible option with its selected value. Change an option, and the list updates, including options that the product rules change as a result.

Make it your own​

  • Show only some attributes. Filter on attribute.identifier to show a fixed list of specifications in the order you want.
  • Use metadata. If you add metadata to attributes in Expivi, read it with useAttribute(configurator, attribute.cid).meta, for example to mark which attributes belong in the specifications. See the useAttribute reference.
  • Show images. Selected values have a thumbnailUrl for color swatches or material images.