I’m having trouble with
We want to run a “bring a friend” action for our services. So we want to offer customers a free session IF they book 2 times the same session at exactly the same time. It can also be a fixed discount (the price of one session), or one free. Both work.
Working in
Wix studio editor, automations → functions
Site link
What I’m trying to do
See above. I’m trying the functions as custom discount rule, but it does not work. I did an output of the payload I get, and then realise I don’t have anything to go by:
{
"purchaseContext": {
"lineItems": [
{
"id": "00000000-0000-0000-0000-000000000001",
"quantity": 1,
"catalogReference": {
"catalogItemId": "xxxxxx-04cb-4348-95d5-f61a86f3f1dd",
"appId": "1d2djlsk-b5ec-5912-8397-c3a5ddb27a97"
},
"price": "17.00"
},
{
"id": "00000000-0000-0000-0000-000000000002",
"quantity": 1,
"catalogReference": {
"catalogItemId": "xxxxxx-111b-4e39-922f-0c20aa906d87",
"appId": "1d2djlsk-b5ec-5912-8397-c3a5ddb27a97"
},
"price": "17.00"
},
{
"id": "00000000-0000-0000-0000-000000000003",
"quantity": 1,
"catalogReference": {
"catalogItemId": "xxxxxxx6-2463-4629-bf04-8de488ae63a6",
"appId": "1d2djlsk-b5ec-5912-8397-c3a5ddb27a97"
},
"price": "17.00"
}
],
"purchaseFlowId": "032jVl8-630f-4cdc-ba89-71320d0d856a"
}
}
For context: the appid is always the same, the catalogItemId is always different, even for the 2 first items which are same service, same time…
What I’ve tried so far
tried if i could do api calls to fetch the data via catalogItemId but that is not possible. I don’t know at this point. And this code, but that was before I realised that the options etc were not set with anything usable:
/**
* @param {Payload} payload
* @returns {boolean}
*/
export default function (payload) {
if (!payload.purchaseContext?.lineItems) return false;
const seen = new Set();
return payload.purchaseContext.lineItems.some(item => {
const ref = item.catalogReference;
const itemId = ref?.catalogItemId;
// Wix Bookings puts slot time in options — try both known field names
const options = ref?.options ?? {};
// @ts-ignore
const key = `${itemId}|${options.slot?.startDate}|${options.startTime}|${options.slotStartTime}`;
if (seen.has(key)) return true;
seen.add(key);
return false;
});
}