Pricing Calculations
A workflow experience computes a number of different pricing components as you modify it. It will return prices to you in the smallest unit of measurement within your active currency code. For example, if your active currency code is USD, the price will be in cents. $1.00 would be represented as 100.
To get the base price of the product, the following function can be used. This is the price of the product before any selections are made. This price is also used in unpersonalised products.
const total = experience.getBasePriceSubunits();
To get the price of all selections made within the workflow, the following function can be used. A selection is a single choice made within a workflow experience. For example, if you have a workflow experience that allows you to choose between a small, medium, or large t-shirt, each of those choices could be a selection. Pricing for selections is stored within variants.
const total = experience.getSelectionPriceSubunits();
To see the total combined price, you can use the following. This sums together the base product price and the selections price.
const total = experience.getTotalPriceSubunits();
Because these values are in subunits you will want to display it in a more human readable way. A function such as the following may be of assistance in doing so.
function formatCentsToDollars(cents) {
// Convert cents to dollars
const dollars = cents / 100;
// Format dollars with currency symbol and commas
const formattedDollars = dollars.toLocaleString("en-US", {
style: "currency",
currency: "USD",
});
return formattedDollars;
}
// Usage example
const cents = 2500;
const formattedAmount = formatCentsToDollars(cents);
console.log(formattedAmount);
For multinational stores this can be a little more complicated as there are quite a few ways currencies need to be represented. In this case we recommend using a third party library such as Dinero.js to simplify the process and make dealing with the values easier. We don't make any assumptions on the approach here as we want to keep our bundle size down. If you have a preferred library or approach we'd love to hear about it.