Skip to main content

Show the price

In this tutorial you show the price of the configuration and keep it up to date while the shopper configures. You add two things: the ready-made <xpv-price-breakdown> component, and a price box of your own.

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

Before you start: set up prices for the product in Expivi. Without prices, the total is 0.

How the price works​

usePriceBreakdown(configurator) returns a price controller. After every change in the configuration, once the product rules have run, the controller asks the Expivi API for the new price. It gives you:

  • data: the current price breakdown, or null while there is none.
  • subscribe(callback): calls callback with the new breakdown after every update. Returns a function that stops the updates.
  • subscribeBusy(callback): calls callback with true while a new price is being calculated, and false when it is done.
  • formatPrice(amount): formats an amount in the configurator's currency and locale, such as €1,437.00.
  • destroy(): stops the updates when you no longer need the controller.

The breakdown has a total and a groups object that splits the total by where the amounts come from: base_price, attributes, attribute_values, script_engine and bom. Each group has its items and a subtotal.

1. Register the price breakdown component​

import { defineCustomElements as definePriceBreakdownElements } from '@expivi/sdk/price-breakdown/components';

definePriceBreakdownElements();

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 price box. They then share one price request per change.

src/pages/price.js
import '../shared/setup.js';
import { usePriceBreakdown } from '@expivi/sdk';
import { defineCustomElements as definePriceBreakdownElements } from '@expivi/sdk/price-breakdown/components';

import { mountPriceBox } from '../components/price-box.js';
import { createExpiviElement, mountProductConfigurator, startConfigurator } from '../shared/configurator.js';
import { renderShopHeader } from '../shared/navigation.js';

renderShopHeader('price');
definePriceBreakdownElements();

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

// One controller feeds both the custom price box and the ready-made breakdown,
// so the price is fetched once per change.
const priceController = usePriceBreakdown(configurator);

mountPriceBox(document.querySelector('#price-box'), priceController);
document
.querySelector('#price-breakdown')
.append(createExpiviElement('xpv-price-breakdown', { controller: priceController }));
},
});

startConfigurator() and mountProductConfigurator() are small helpers in src/shared/configurator.js. They contain the code from Add Expivi to a web page: create the configurator, load the product and add the visualizer and the options once the product is READY.

3. Build your own price box​

The price box shows the total and dims it while a new price is being calculated.

src/components/price-box.js
/**
* Shows the total price of the configuration and updates it whenever the price changes.
*
* @param {HTMLElement} container
* @param {import('@expivi/sdk').PriceBreakdownController} priceController from usePriceBreakdown(configurator)
* @returns {() => void} stops updating
*/
export function mountPriceBox(container, priceController) {
container.innerHTML = `
<div class="price-box">
<span class="price-box__label">Total price</span>
<strong class="price-box__amount">–</strong>
</div>`;

const box = container.querySelector('.price-box');
const amount = container.querySelector('.price-box__amount');

function renderPrice(priceBreakdown) {
// formatPrice() uses the configurator's currency and locale.
amount.textContent = priceBreakdown ? priceController.formatPrice(priceBreakdown.total) : '–';
}

renderPrice(priceController.data);
const unsubscribePrice = priceController.subscribe(renderPrice);
// The price is recalculated after each change; dim it while that is in progress.
const unsubscribeBusy = priceController.subscribeBusy((isBusy) => box.classList.toggle('price-box--busy', isBusy));

return () => {
unsubscribePrice();
unsubscribeBusy();
};
}

Result​

The price box and the breakdown show the same total. Change an option, and both update.

The ready-made <xpv-price-breakdown> lists the total and the amounts per group. Its labels are in English. If you need other texts or a different layout, build your own breakdown from the groups in the controller's data, like the price box does with total.

See also​