Limit number of available bookings in A+ per M2P membership

The idea is to limit the number of bookings a user can make in a month depending on the membership he/she is a member of. That doesn't seem doable "out of the box" but should be possible with some additional code - probably similar, bot not the same, to this one: https://premium.wpmudev.org/forums/topic/appointments-and-membership-limit-number-of-bookings-user-group#post-894041 The difference here that it shouldn't drop/change membership but just stop allowing further bookings for the member until the next month of the subscription starts.

Will I it be something that 2nd-line support can help me with or would that be a custom development job?

  • Majid

    Hello Asrar !

    I hope you are doing great today,

    The following code can help you achieve what you are looking for, however it only works when the user only have one membership

    // Limit number of Appointments per Membership
    function limit_apps( $reply_array ) {
    	global $wpdb;
    	// the limit of each membership
    	$count_per_month = array(
    		123 => 2, // membership id => number of appointments per month
    	);
    	$app_table = $wpdb->prefix . 'app_appointments';
    	$current_user = wp_get_current_user();
    	$current_user_id = $current_user->ID;
    	$member = MS_Factory::load( 'MS_Model_Member', $current_user_id );
    	$membership_ids = $member->get_membership_ids();
    	$membership_id = null;
    
    	// get first membership
    	if( ! empty( $membership_ids ) && is_array( $membership_ids ) ) {
    		$membership_id = $membership_ids[0];
    	}
    
    	// check if the memberrship id is in the count array
    	if( $membership_id && array_key_exists( $membership_id , $count_per_month ) ) {
    
    		// get the number of already booked appointements
    		$count = $wpdb->get_var( "SELECT COUNT(*) FROM " . $app_table . " WHERE user=" . $current_user_id . " AND (status='pending' OR status='confirmed' OR status='paid' ) and created > DATE_SUB(CURDATE(), INTERVAL 30 DAY)" );
    
    		// return if the number reached the maximum
    		if( $count >= $count_per_month[$membership_id] ) {
    			return array( 'error'=>'You have reached maximum allowed number of appointments' );
    		}
    
    	}
    	return $reply_array;
    }
    add_filter( 'app_pre_confirmation_reply', 'limit_apps', 10 );

    Everything relies on this piece of code here

    $count_per_month = array(
    		123 => 2, // membership id => number of appointments per month
    	);

    as the 123 is the membership id and the 2 is the limit of appointment, you need to change that to you MS id, if you have multiple, you can add multiple lines so it will look like this

    $count_per_month = array(
    		123 => 2, // membership id => number of appointments per month
                    40 => 6,
                  212 => 3
    	);

    Add it to your theme functions.php file or inside an mu plugin.

    Let me know how it went :slight_smile:

    Cheers,
    Majid

  • Asrar

    Hello,

    Dear Majid - Salam,

    i tried its work,

    but i need it work with more then one membership and it seems limited to only one Services i have more then 10+ services.

    Example:
    Membership Group 1 : allowed to book. 2 X Pest Control and 3 X house Cleaning Appointment per month.
    Membership Group 2 : allowed to book. 5 X Pest Control and 7 X house Cleaning Appointment per month.

    If i missed anything let me know.

    Thank You,

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.