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, ornullwhile there is none.subscribe(callback): callscallbackwith the new breakdown after every update. Returns a function that stops the updates.subscribeBusy(callback): callscallbackwithtruewhile a new price is being calculated, andfalsewhen 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.
- Vanilla JS
- Vue
- React
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.
The page renders PricePanels once the configurator is READY (see ProductConfiguratorLayout.vue), so PricePanels can create the controller right away:
<script setup lang="ts">
import { onBeforeUnmount } from 'vue';
import { usePriceBreakdown, type ProductConfigurator } from '@expivi/sdk';
import PriceBox from './PriceBox.vue';
/** Rendered once the product is READY, so it can create its controller right away. */
const props = defineProps<{ configurator: ProductConfigurator }>();
// One controller feeds both the custom price box and the ready-made breakdown,
// so the price is fetched once per change.
const priceController = usePriceBreakdown(props.configurator);
onBeforeUnmount(() => priceController.destroy());
</script>
<template>
<section class="panel">
<h2 class="panel__title">Your price</h2>
<!-- A custom price box, built with usePriceBreakdown(). -->
<PriceBox :controller="priceController" />
</section>
<section class="panel">
<h2 class="panel__title">Price breakdown</h2>
<!-- The ready-made <xpv-price-breakdown> component. -->
<xpv-price-breakdown .controller="priceController" />
</section>
</template>
The page renders PricePanels once the configurator is READY (see ProductConfiguratorLayout.tsx). useExpiviController creates the controller in an effect and destroys it on unmount:
import { usePriceBreakdown, type ProductConfigurator } from '@expivi/sdk';
import { useExpiviController } from '../hooks/useExpiviController';
import PriceBox from './PriceBox';
/** Rendered once the product is READY. */
export default function PricePanels({ configurator }: { configurator: ProductConfigurator }) {
// One controller feeds both the custom price box and the ready-made breakdown,
// so the price is fetched once per change.
const priceController = useExpiviController(() => usePriceBreakdown(configurator), [configurator]);
if (!priceController) return null;
return (
<>
<section className="panel">
<h2 className="panel__title">Your price</h2>
{/* A custom price box, built with usePriceBreakdown(). */}
<PriceBox controller={priceController} />
</section>
<section className="panel">
<h2 className="panel__title">Price breakdown</h2>
{/* The ready-made <xpv-price-breakdown> component. */}
<xpv-price-breakdown controller={priceController} />
</section>
</>
);
}
3. Build your own price box
The price box shows the total and dims it while a new price is being calculated.
- Vanilla JS
- Vue
- React
/**
* 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();
};
}
<script setup lang="ts">
import { onBeforeUnmount, ref } from 'vue';
import type { PriceBreakdown, PriceBreakdownController } from '@expivi/sdk';
/** Shows the total price of the configuration and updates it whenever the price changes. */
const props = defineProps<{ controller: PriceBreakdownController }>();
const priceBreakdown = ref<PriceBreakdown | null>(props.controller.data);
const isBusy = ref(props.controller.isBusy);
const unsubscribePrice = props.controller.subscribe((data) => (priceBreakdown.value = data));
// The price is recalculated after each change; dim it while that is in progress.
const unsubscribeBusy = props.controller.subscribeBusy((busy) => (isBusy.value = busy));
onBeforeUnmount(() => {
unsubscribePrice();
unsubscribeBusy();
});
</script>
<template>
<div :class="['price-box', { 'price-box--busy': isBusy }]">
<span class="price-box__label">Total price</span>
<!-- formatPrice() uses the configurator's currency and locale. -->
<strong class="price-box__amount">
{{ priceBreakdown ? controller.formatPrice(priceBreakdown.total) : '–' }}
</strong>
</div>
</template>
import { useEffect, useState } from 'react';
import type { PriceBreakdown, PriceBreakdownController } from '@expivi/sdk';
/** Shows the total price of the configuration and updates it whenever the price changes. */
export default function PriceBox({ controller }: { controller: PriceBreakdownController }) {
const [priceBreakdown, setPriceBreakdown] = useState<PriceBreakdown | null>(controller.data);
const [isBusy, setIsBusy] = useState(controller.isBusy);
useEffect(() => {
setPriceBreakdown(controller.data);
const unsubscribePrice = controller.subscribe(setPriceBreakdown);
// The price is recalculated after each change; dim it while that is in progress.
const unsubscribeBusy = controller.subscribeBusy(setIsBusy);
return () => {
unsubscribePrice();
unsubscribeBusy();
};
}, [controller]);
return (
<div className={`price-box${isBusy ? ' price-box--busy' : ''}`}>
<span className="price-box__label">Total price</span>
{/* formatPrice() uses the configurator's currency and locale. */}
<strong className="price-box__amount">{priceBreakdown ? controller.formatPrice(priceBreakdown.total) : '–'}</strong>
</div>
);
}
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
usePriceBreakdownreference- The complete product page puts the price box in a "buy box" with a quote button.