Limit number of appointments for specific membership

What we are trying to achieve is, allow 100% discount on appointments for members of a specific membership. And at the same time, restrict the number of appointments those members can make to three(3).

For example, membership A is set with 100% discount in Appointments+ settings. User register and pay for membership A, which grant her access to book service B(which is paid) for free.

But after the user has booked three appointments, she’s unable to take other appointments.

Was provided some code in support chat to achieve that but doesn’t seem to work.