Search.../

calculatePrice( )

Calculates the price of a booking to include during checkout. The calculated price is based on custom business logic.

Description

The calculatePrice() function calculates custom pricing for use on the checkout pages of a merchant's site. The function is automatically called by Wix when the booking is being checked out for payment.

Where to find calculatePrice()

When you add the Custom Pricing extension, a folder is automatically added to your site. Use the <my-extension-name>.js file in the folder to write the code to calculate the price for the booking.

For more information on customizing your price calculation, see Tutorial: Bookings Pricing Custom Extension.

Syntax

function calculatePrice(options: Options): Promise<CalculatePriceResponse>

calculatePrice Parameters

NAME
TYPE
DESCRIPTION
options
Options

Options for calculating the price, including the actual booking.

Returns

Fulfilled - The calculated price for the booking.

Return Type:

Promise<CalculatePriceResponse>
NAME
TYPE
DESCRIPTION
calculatedPrice
number

The price for the booking according to your custom logic.

The calculatePrice() response is based on your site's currency.

Was this helpful?

Calculate a custom price for a booking

This example calculates the custom price for booking a massage using cheaper rates for weekdays and with additional options.

Copy Code
1// Import "coded-elsewhere" functionality for retrieving special rates
2import { getPrices } from 'backend/queries';
3
4// The calculatePrice SPI runs at checkout to calculate the final price
5export const calculatePrice = async (options) => {
6 const prices = getPrices();
7
8 const additionalFields = options.booking.additionalFields;
9 const numberOfParticipants = options.booking.numberOfParticipants;
10 const weekendRate = getFieldValue(additionalFields, "weekendRate");
11 const addon1Value = getFieldValue(additionalFields, "Addon 1");
12 const addon2Value = getFieldValue(additionalFields, "Addon 2");
13 const addon3Value = getFieldValue(additionalFields, "Addon 3");
14 const rate = weekendRate == "true" ? prices.weekend : prices.weekday;
15
16 const finalPrice = numberOfParticipants * rate +
17 addon1Value * prices.addon1 +
18 addon2Value * prices.addon2 +
19 addon3Value * prices.addon3
20 ;
21
22 return {calculatedPrice: finalPrice};
23};
24
25// Retrieve the charge for each given `additionalField` for the booking.
26export function getFieldValue(additionalFields, text) {
27 const foundFieldArray = additionalFields.filter(additionalField => additionalField.label === text);
28 return foundFieldArray.length === 0 ? undefined : foundFieldArray[0].value;
29}