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.

  • Dimitris
    • Support Star

    Hello there shelvene,

    hope you're doing good today! :slight_smile:

    In order to provide discounts for users that's part of a Membership level, you just have to go to Appointments -> Settings -> Genaral -> Payments and set it to 100% for the membership level(s) you need.

    In order to limit the appointments you will need to create a new MU plugin file like wp-content/mu-plugins/app-limit.php (just create the /mu-plugins/ folder, if it doesn't exist already) and insert the exact snippet in there:

    <?php
    // works only for logge in users!!!
    function limit_active_apps( $reply_array ) {
    	global $wpdb, $appointments;
    
    	# SETTINGS
    	$number_of_allowed_slots_per_day = 3; // a user can book no more appointments for one day
    	$number_of_days_ahead = 7; // user cannot book past the "today + this number of days" date
    	$membership_level = 1344; // membership level ID
    
    	// get booking user ID
    	$current_user = wp_get_current_user();
    	$current_user_id = $current_user->ID;
    	// set appointments table
    	$app_table = $wpdb->prefix . 'app_appointments';
    
    	// get start date of booked appointment
    	$datetime = strip_tags( $reply_array['start'] );
    	$datetime = explode( ': ', $datetime);
    	$datetime = explode( ' ', $datetime[1]);
    
    	// limit bookings to up to $number_of_days_ahead days into the future
    	if ( (time() + ($number_of_days_ahead * 24 * 60 * 60)) < strtotime( str_replace( ',', '', $appointments->to_us( $datetime[0] ) ) ) ) {
    		return array ( 'error' => 'Selected date is in over ' . $number_of_days_ahead . ' days from now. Please book within ' . $number_of_days_ahead . ' days from today!' );
    	}
    
    	//convert date to database
    	$date = date( 'Y-m-d', strtotime( str_replace( ',', '', $appointments->to_us( $datetime[0] ) ) ));
    
    	//get current member from Membership 2 Pro
    	$member = MS_Model_Member::get_current_member();
    
    	// count appointments for $date already booked by user.
    	$query = "SELECT COUNT(*) FROM " . $app_table . " WHERE user=" . $current_user_id . " AND (status='pending' OR status='confirmed' OR status='paid' ) AND start LIKE '" . $date . "%'";
    	$count = $wpdb->get_var( $query );
    
    	if ( ( $count ) && ( $count > ( $number_of_allowed_slots_per_day - 1) ) && ( $member->has_membership( $membership_level ) ) ) {
    		return array ( 'error' => 'You have already booked ' . $count . ' appointments for this day. Try another day!' );
    	}	
    
    	return $reply_array;
    }
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

    Please keep in mind to set the membership level you need in this line as that number is coming from my tests. :wink:
    $membership_level = 1344; // membership level ID

    Let us know how that goes!
    Warm regards,
    Dimitris

  • shelvene
    • Design Lord, Child of Thor

    Hi Dimitri,
    Everything is fine.
    Thank you, for the Mu-plugin, but when I integrate it, I get a blank page.
    I must point out that another Mu-plugin has already been created by Patrick
    (moreover, the limitation to 3, is for one year):

    <?php
    /*
    Plugin Name: Limit appointments
    Plugin URI: https://premium.wpmudev.org/
    Description: Limits appointments to max 3 per 12 months for Membership 18077
    */
    // Works only if login required
    function limit_active_apps( $reply_array ) {
    global $wpdb, $current_user, $appointments;
    // Change statuses as required. i.e. remove pending condition if you wish
    $count = $wpdb->get_var( "SELECT COUNT(*) FROM " . $appointments->app_table . " WHERE user=".$current_user->ID." AND (status='pending' OR status='confirmed' OR status='paid' ) and start > NOW() - INTERVAL 365 DAY " );
    $member = MS_Model_Member::get_current_member();
    if ( ( $count >= 3) && ( $member->has_membership( 18077 ) ) )
    return array( 'error'=>'You have reached maximum allowed number of appointments' );
    return $reply_array;
    }
    if ( apply_filters( 'ms_active', false ) ) {
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );
    }

    The membeship subscription is done for one year and that work fine. So there is still the problem of the 3 free appointments.
    I have set the payment in A+ with 100% discount.

    Have a look.
    Wouldn't it be possible to work by chat and try it live?
    Thanks for helping.
    Shelvene

  • Dimitris
    • Support Star

    Hello Shelvene

    Wouldn't it be possible to work by chat and try it live?

    As long as an issue requires a bit more time to be resolved, we move from live chat to support forums. :slight_smile: When doing so, we're keen on continuing in the forum thread exclusively.

    Thank you, for the Mu-plugin, but when I integrate it, I get a blank page.
    I must point out that another Mu-plugin has already been created by Patrick
    (moreover, the limitation to 3, is for one year)

    Please remove the MU plugin that Patrick shared and use only the one I posted in my previous reply. In order to lock users for one year, edit this line
    $number_of_days_ahead = 7; // user cannot book past the "today + this number of days" date
    to
    $number_of_days_ahead = 364; // user cannot book past the "today + this number of days" date

    Let me know how that goes!
    Thank you,
    Dimitris

  • shelvene
    • Design Lord, Child of Thor

    Hello Dimitris,
    This work fine but, finally, I won't use it because Patrick's solution (he imagined to help me) doesn't work properly. Besides, it's too complicated.
    I explain my problem again: I use A+ to manage 2 types of services:
    - 1 single service with a unit payment (work well)
    - 1 package with payment for 3 sessions (which must be taken within the year)
    That's my problem; I can't set up this second service.
    If you can help me with code, that would be great.
    Regards
    Shelvene

  • Dimitris
    • Support Star

    Hello there shelvene,

    hope you're doing good today! :slight_smile:

    I'm not quite following you here, do you mean that you want to exclude Membership 2 Pro plugin and accomplish this only with Appointments+ plugin?

    Or maybe you need above functionality with custom code, to be working for a specific Service only, as well as the membership discount should be for the specific Service?

    Please advise!
    Warm regards,
    Dimitris

  • Dimitris
    • Support Star

    Hello there shelvene,

    hope you're doing good today! :slight_smile:

    Unfortunately this would require some extended custom development, thing that surpasses the scope of our support. If you absolutely need this kind of custom work, then you can make advantage of our partnership with Codeable here: https://premium.wpmudev.org/partners/
    Reference: https://premium.wpmudev.org/docs/getting-started/getting-support/#chapter-1

    Warm regards,
    Dimitris

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.