Custom discount rule when booking 2 times same service at exact same time

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;
  });
}

Hey, I’ve been working with the discount rules API although not with bookings, I may still be able to point you in the right direction.
Your initial approach of triggering the discount conditionally in a custom discount should work although without seeing the full discount rule code I can’t say for certain it’s correct.

However I quickly looked through the docs and apparently the booking ID which would be unique to every booking is what is placed under catalogItemId so that ID isn’t directly linked to the generic booking object that you are trying to query from as in the function.

The generic advice I would give is your custom discount rule would need to extract the catalogItemId directly from each line item, then query the bookings api to find out what the actual booking data is for that specific booking ID, then create a map with the generic ‘Booking Event Id’ and “Booked Time” as a stringified key to organize your bookings and find if there are any duplicates.

Hopefully that’s helpful, lmk if you get stuck