Appointments+ and Membership limit number of bookings user group

I found the below thread helpful. I have added the code to my child function php which will allow me to restrict the number of bookings per month dependent on the user group in membership; however, is there a way to combine this code and another code so that a member can either book a limited number of appointments in a month OR book a set amount of appointments then have subscription end.

Example: Membership Group 1 pays for 2 bookings a month for 3 months and uses the code below. Group 2 pays for 6 bookings whenever they want to use them but once all 6 bookings are made subscription expires.

https://premium.wpmudev.org/forums/topic/restrict-number-of-bookings-per-month-per-user-group

Code in my functions php will look something like this

// 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 30 DAY " );
if(current_user_on_level( '1' ) && $count>4){return array( 'error'=>'You have reached 4 bookings max' ); return $reply_array;}
if(current_user_on_level( '2' ) && $count>3){return array( 'error'=>'You have reached 3 bookings max' ); return $reply_array;}
if(current_user_on_level( '3' ) && $count>2){return array( 'error'=>'You have reached 2 bookings max' ); return $reply_array;}
if(current_user_on_level( '4' ) && $count>2){return array( 'error'=>'You have reached 2 bookings max' ); return $reply_array;}
if(current_user_on_level( '5' ) && $count>4){return array( 'error'=>'You have reached 4 bookings max' ); return $reply_array;}
if(current_user_on_level( '6' ) && $count>4){return array( 'error'=>'You have reached 4 bookings max' ); return $reply_array;}
  • Ash

    Hello @Andy

    I hope you are well today and thanks for asking the question.

    I am sorry I didn't understand the question :slight_frown:

    is there a way to combine this code and another code so that a member can either book a limited number of appointments in a month OR book a set amount of appointments then have subscription end.

    So, in here -
    "a member can either book a limited number of appointments in a month" - it's done in your above code right?

    and "book a set amount of appointments then have subscription end" - would you please explain this?

    Cheers
    Ash

  • voltaicrose

    Hi @Ash

    Sorry if my question was not clear enough. My above code does limit the number of appointments in a month. What I want is to add some code that will also make it so that if current user is on level 6 and count is => to 4 then it triggers an an event that would switch the membership level to 1 instead of 6 in membership and then the return array would say something like "you have booked the maximum amount of appointments allowed for this subscription."

    This would make it so that after 4 appointments the user could still book appointments but would have to pay full price for each appointment since I have it set so that each appointment while in membership level 6 is discounted 100%.

    I hope this clarifies the issue.

  • Hoang Ngo

    Hi @voltaicrose,

    I hope you are well today.

    I think you can use something like this

    add_action('app_new_appointment', 'drop_membership_when_app_limit');
    function drop_membership_when_app_limit($app_id)
    {
        if (!is_user_logged_in()) {
            return '';
        }
    
        global $wpdb, $appointments;
        //select app from this user in 30 days
        $count = $wpdb->get_var("SELECT COUNT(*) FROM " . $appointments->app_table . " WHERE user=" . get_current_user_id() . " AND (status='pending' OR status='confirmed' OR status='paid' ) and start > NOW() - INTERVAL 30 DAY ");
        //example, we have user on level 6, and max is 4
    ///////block
        if (current_user_on_level(6) && $count >= 4) {
            $member = new Membership_Model_Member(get_current_user_id());
            $member->move_level(6, 1);
            wp_send_json(array(
                'error' => 'Your error here'
            ));
        }
    //////endblock
    }

    If you want to apply to other level, please copy the block and update the value. Let's see if it help for you :slight_smile:

    Best regards,
    Hoang

  • Hoang Ngo

    Hi @voltaicrose,

    I hope you are well today.

    For membership 2, I think something like this should work

    add_action('app_new_appointment', 'drop_membership_when_app_limit');
    function drop_membership_when_app_limit($app_id)
    {
    	if (!is_user_logged_in()) {
    		return '';
    	}
    
    	global $wpdb, $appointments;
    	//select app from this user in 30 days
    	$count = $wpdb->get_var("SELECT COUNT(*) FROM " . $appointments->app_table . " WHERE user=" . get_current_user_id() . " AND (status='pending' OR status='confirmed' OR status='paid' ) and start > NOW() - INTERVAL 30 DAY ");
    	//example, we have user on level 6, and max is 4
    ///////block
    
    	$member = new MS_Model_Member(get_current_user_id());
    	if($member->has_membership(YOUR_MEMBERSHIP_ID_HERE) && $count >=4){
    		$member->move_membership(YOUR_MEMBERSHIP_ID_HERE,NEW_MEMBERSHIP_ID);
    		wp_send_json(array(
    			'error' => 'Your error here'
    		));
    	}
    //////endblock
    }

    Please replace the YOUR_MEMBERSHIP_ID_HERE & NEW_MEMBERSHIP_ID to suit your need.

    If you have any issues please don't hesitate to let us know so we can assist.

    Best regards,
    Hoang

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.