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 aname, theproductCidof its product, ahiddenflag, and its selected values inconfiguredValue.
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.
- Vanilla JS
- Vue
- React
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;
}
import { useStoreAttributes, useStoreProductOffers, type ProductConfigurator } from '@expivi/sdk';
export interface SummaryRow {
cid: string;
label: string;
value: string;
}
export interface ProductSummary {
productCid: string;
productName: string;
rows: SummaryRow[];
}
type ConfiguredValue = { value: unknown; description: string; identifier: string };
/**
* 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.
*/
export function getConfigurationSummary(configurator: ProductConfigurator): ProductSummary[] {
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: ProductConfigurator,
callback: (summary: ProductSummary[]) => void,
): () => void {
const update = () => callback(getConfigurationSummary(configurator));
const unsubscribeAttributes = useStoreAttributes(configurator).subscribe(update);
const unsubscribeProducts = useStoreProductOffers(configurator).subscribe(update);
update();
return () => {
unsubscribeAttributes();
unsubscribeProducts();
};
}
function formatConfiguredValue({ value, description, identifier }: ConfiguredValue): string {
if (typeof value === 'string' || typeof value === 'number') return String(value);
if (typeof value === 'boolean') return value ? 'Yes' : 'No';
return description || identifier;
}
import { useStoreAttributes, useStoreProductOffers, type ProductConfigurator } from '@expivi/sdk';
export interface SummaryRow {
cid: string;
label: string;
value: string;
}
export interface ProductSummary {
productCid: string;
productName: string;
rows: SummaryRow[];
}
type ConfiguredValue = { value: unknown; description: string; identifier: string };
/**
* 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.
*/
export function getConfigurationSummary(configurator: ProductConfigurator): ProductSummary[] {
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: ProductConfigurator,
callback: (summary: ProductSummary[]) => void,
): () => void {
const update = () => callback(getConfigurationSummary(configurator));
const unsubscribeAttributes = useStoreAttributes(configurator).subscribe(update);
const unsubscribeProducts = useStoreProductOffers(configurator).subscribe(update);
update();
return () => {
unsubscribeAttributes();
unsubscribeProducts();
};
}
function formatConfiguredValue({ value, description, identifier }: ConfiguredValue): string {
if (typeof value === 'string' || typeof value === 'number') return String(value);
if (typeof value === 'boolean') return value ? 'Yes' : 'No';
return description || identifier;
}
2. Render the summary
Render the summary once the configurator is READY, and update it from subscribeConfigurationSummary().
- Vanilla JS
- Vue
- React
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('');
});
}
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);
},
});
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue';
import type { ProductConfigurator } from '@expivi/sdk';
import { subscribeConfigurationSummary, type ProductSummary } from '../shared/summary';
/** Lists the selected options per product and updates the list while the shopper configures. */
const props = defineProps<{ configurator: ProductConfigurator }>();
const summary = ref<ProductSummary[]>([]);
const unsubscribe = subscribeConfigurationSummary(props.configurator, (newSummary) => (summary.value = newSummary));
onBeforeUnmount(unsubscribe);
</script>
<template>
<section v-for="product in summary" :key="product.productCid" class="summary__product">
<h3 class="summary__product-name">{{ product.productName }}</h3>
<dl class="summary__list">
<div v-for="row in product.rows" :key="row.cid" class="summary__row">
<dt>{{ row.label }}</dt>
<dd>{{ row.value }}</dd>
</div>
</dl>
</section>
</template>
<script setup lang="ts">
import ConfigurationSummary from '../components/ConfigurationSummary.vue';
import ProductConfiguratorLayout from '../components/ProductConfiguratorLayout.vue';
import ShopLayout from '../components/ShopLayout.vue';
import { useExpiviConfigurator } from '../composables/useExpiviConfigurator';
const { configurator, hasError, missingConfig } = useExpiviConfigurator();
</script>
<template>
<ShopLayout page-id="specifications">
<ProductConfiguratorLayout :configurator="configurator" :has-error="hasError" :missing-config="missingConfig">
<template #before-options="{ configurator }">
<section class="panel">
<h2 class="panel__title">Specifications</h2>
<ConfigurationSummary :configurator="configurator" />
</section>
</template>
</ProductConfiguratorLayout>
</ShopLayout>
</template>
import { useEffect, useState } from 'react';
import type { ProductConfigurator } from '@expivi/sdk';
import { subscribeConfigurationSummary, type ProductSummary } from '../shared/summary';
/** Lists the selected options per product and updates the list while the shopper configures. */
export default function ConfigurationSummary({ configurator }: { configurator: ProductConfigurator }) {
const [summary, setSummary] = useState<ProductSummary[]>([]);
useEffect(() => subscribeConfigurationSummary(configurator, setSummary), [configurator]);
return summary.map((product) => (
<section key={product.productCid} className="summary__product">
<h3 className="summary__product-name">{product.productName}</h3>
<dl className="summary__list">
{product.rows.map((row) => (
<div key={row.cid} className="summary__row">
<dt>{row.label}</dt>
<dd>{row.value}</dd>
</div>
))}
</dl>
</section>
));
}
import ConfigurationSummary from '../components/ConfigurationSummary';
import ProductConfiguratorLayout from '../components/ProductConfiguratorLayout';
import ShopLayout from '../components/ShopLayout';
import { useExpiviConfigurator } from '../hooks/useExpiviConfigurator';
export default function SpecificationsPage() {
const { configurator, hasError, missingConfig } = useExpiviConfigurator();
return (
<ShopLayout pageId="specifications">
<ProductConfiguratorLayout configurator={configurator} hasError={hasError} missingConfig={missingConfig}
beforeOptions={(configurator) => (
<section className="panel">
<h2 className="panel__title">Specifications</h2>
<ConfigurationSummary configurator={configurator} />
</section>
)}
/>
</ShopLayout>
);
}
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.identifierto 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 theuseAttributereference. - Show images. Selected values have a
thumbnailUrlfor color swatches or material images.