Calculates the price of a booking to include during checkout. The calculated price is based on custom business logic.
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.
calculatePrice()
When you add the Custom Pricing plugin,
a folder is automatically added to your site. Use the .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.
function calculatePrice(
options: Options,
context: Context,
): Promise<CalculatePriceResponse>;
Options for calculating the price, including the actual booking.
Metadata about the request.
This example calculates the custom price for booking a massage using cheaper rates for weekdays and with additional options.
// Import "coded-elsewhere" functionality for retrieving special rates
import { getPrices } from "backend/queries";
// The calculatePrice service plugin runs at checkout to calculate the final price
export const calculatePrice = async (options) => {
const prices = getPrices();
const additionalFields = options.booking.additionalFields;
const numberOfParticipants = options.booking.numberOfParticipants;
const weekendRate = getFieldValue(additionalFields, "weekendRate");
const addon1Value = getFieldValue(additionalFields, "Addon 1");
const addon2Value = getFieldValue(additionalFields, "Addon 2");
const addon3Value = getFieldValue(additionalFields, "Addon 3");
const rate = weekendRate == "true" ? prices.weekend : prices.weekday;
const finalPrice =
numberOfParticipants * rate +
addon1Value * prices.addon1 +
addon2Value * prices.addon2 +
addon3Value * prices.addon3;
return { calculatedPrice: finalPrice };
};
// Retrieve the charge for each given `additionalField` for the booking.
export function getFieldValue(additionalFields, text) {
const foundFieldArray = additionalFields.filter(
(additionalField) => additionalField.label === text,
);
return foundFieldArray.length === 0 ? undefined : foundFieldArray[0].value;
}
This method doesn’t return any custom errors, but may return standard errors. Learn more about standard Wix errors.