Automatically book customers into scheduled sessions when they purchase a payment plan

I’ve seen a couple of old unanswered posts about this, maybe things have changed since then!

Basically, I want to sell a monthly payment plan to scheduled sessions, and when the subscription is active (recurring payment not just on plan purchased) they are added to the sessions. I don’t want them to have to book themselves in to each individual session.

On wix editor I’ve tried creating a CMS called classesCollection to map the planId to the sessionId, and the following backend code

import { Permissions, webMethod } from "wix-web-module";
import wixData from 'wix-data';
import { bookings as bookingsBackend } from "wix-bookings-backend";
import { bookings as bookingsV2 } from 'wix-bookings.v2';

export function wixPricingPlans_onOrderCreated(event) {
    const { planId, buyer } = event.entity;

    handlePlanBooking(planId, buyer);
}

export function wixPricingPlans_onOrderCycleStarted(event) {
    const { planId, buyer } = event.entity;

    handlePlanBooking(planId, buyer);
}

function handlePlanBooking(planId, buyer) {
    wixData.query('classesCollection')
        .eq('planId', planId)
        .find()
        .then((result) => {
            const sessions = result.items;

            if (sessions.length === 0) {
                console.warn(`No sessions found for planId: ${planId}`);
                return;
            }

            sessions.forEach((session) => {
                createBooking(session.sessionId, buyer);
            });
        })
        .catch((error) => {
            console.error('Error fetching sessions:', error);
        });
}

function createBooking(sessionId, buyer) {
    bookingsBackend.queryBookings()
        .eq('contactDetails.email', buyer.email)
        .eq('bookedEntity.slot.sessionId', sessionId)
        .find()
        .then((existingBookings) => {
            if (existingBookings.items.length > 0) {
                console.log(`Customer ${buyer.email} is already booked for session: ${sessionId}`);
                return;
            }

            const booking = {
                totalParticipants: 1,
                contactDetails: {
                    firstName: buyer.name.first,
                    lastName: buyer.name.last,
                    email: buyer.email,
                },
                bookedEntity: {
                    slot: { sessionId: sessionId },
                },
            };

            bookingsV2.createBooking(booking)
                .then((newBooking) => {
                    console.log('Booking created successfully:', newBooking);
                })
                .catch((error) => {
                    console.error('Error creating booking:', error);
                });
        })
        .catch((error) => {
            console.error('Error querying bookings:', error);
        });
}

I’ve tried many variations, like waaaaay too many, wouldn’t be surprised if im still trying to solve this in my dreams tonight… any input would be greatly appreciated!

Hi there,

If you would like to automatically book customers, I suggest that you look into this article, it should help the user complete the book flow. The user only create the booking, he should also create Checkout and createOrder().

I hope this helps!

Thanks,
Tal from Wix Bookings