Skip to main content

Show the bill of materials

In this tutorial you show the bill of materials (BOM) of the configuration: the parts that make up the configured product, with their quantities and prices. B2B shoppers and sales teams use it to check what exactly is being ordered. You add the ready-made <xpv-bom-breakdown> component and a table of your own.

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

Before you start: set up a bill of materials for the product in Expivi. Without one, the BOM is empty.

How the BOM works​

useBomBreakdown(configurator) returns a BOM controller. It works like the price controller: after every change in the configuration, it asks the Expivi API for the new BOM. It has data, subscribe(), subscribeBusy(), formatPrice() and destroy().

The BOM has a list of parts. Each part has:

  • name, sku and identifier
  • quantity and uom (unit of measure, such as pcs)
  • pricePerUnit and totalPrice, which can be null
  • thumbnailUrl, which can be null
  • components: the components the part consists of, each with a name, sku, quantity and uom
  • attachments: files such as drawings, each with a name and url

1. Register the BOM component​

import { defineCustomElements as defineBomBreakdownElements } from '@expivi/sdk/bom-breakdown/components';

defineBomBreakdownElements();

2. Create one controller for the page​

Create the controller once the configurator is READY, and give it to both the ready-made component and your own table.

src/pages/bom.js
import '../shared/setup.js';
import { useBomBreakdown } from '@expivi/sdk';
import { defineCustomElements as defineBomBreakdownElements } from '@expivi/sdk/bom-breakdown/components';

import { mountBomTable } from '../components/bom-table.js';
import { createExpiviElement, mountProductConfigurator, startConfigurator } from '../shared/configurator.js';
import { renderShopHeader } from '../shared/navigation.js';

renderShopHeader('bom');
defineBomBreakdownElements();

startConfigurator({
onReady(configurator) {
mountProductConfigurator(configurator);

// One controller feeds both the custom table and the ready-made component.
const bomController = useBomBreakdown(configurator);

mountBomTable(document.querySelector('#bom-table'), bomController);
document
.querySelector('#bom-breakdown')
.append(createExpiviElement('xpv-bom-breakdown', { controller: bomController }));
},
});

3. Build your own BOM table​

src/components/bom-table.js
import { escapeHtml } from '../shared/escape-html.js';

/**
* Shows the bill of materials as a table and updates it whenever the configuration changes.
*
* @param {HTMLElement} container
* @param {import('@expivi/sdk').BomBreakdownController} bomController from useBomBreakdown(configurator)
* @returns {() => void} stops updating
*/
export function mountBomTable(container, bomController) {
function renderBom(bom) {
if (!bom?.parts.length) {
container.innerHTML = '<p class="panel__note">This configuration has no bill of materials.</p>';
return;
}

const rows = bom.parts
.map(
(part) => `
<tr>
<td>${escapeHtml(part.name ?? part.identifier)}</td>
<td>${escapeHtml(part.sku ?? '—')}</td>
<td class="bom-table__number">${part.quantity} ${escapeHtml(part.uom)}</td>
<td class="bom-table__number">${part.pricePerUnit === null ? '—' : bomController.formatPrice(part.pricePerUnit)}</td>
</tr>`,
)
.join('');

container.innerHTML = `
<table class="bom-table">
<thead>
<tr>
<th>Part</th>
<th>SKU</th>
<th class="bom-table__number">Quantity</th>
<th class="bom-table__number">Unit price</th>
</tr>
</thead>
<tbody>${rows}</tbody>
</table>`;
}

renderBom(bomController.data);
return bomController.subscribe(renderBom);
}

Result​

Both the table and the ready-made component list the parts. Change an option that affects the parts, such as a size, and both update.

The ready-made component shows the parts as cards, with thumbnails, components and attachments. Build your own when you need other columns, other texts or, for example, an export to CSV.

See also​