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 Bookings when the booking is being checked out for payment.
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 calculatedPrice()
function's 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 rates2import { getPrices } from 'backend/queries';34// The calculatePrice SPI runs at checkout to calculate the final price5export const calculatePrice = async (options) => {6 const prices = getPrices();78 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;1516 const finalPrice = numberOfParticipants * rate +17 addon1Value * prices.addon1 +18 addon2Value * prices.addon2 +19 addon3Value * prices.addon320 ;2122 return {calculatedPrice: finalPrice};23};2425// 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}