Allow only 2 appointments in a week from a customer

Hello,

is it possible to permit only two appointments from a single customer in one week?
The customer have to log in before adding an appointment. So based on the username should be a possibility to count the amount of appointments in one week.

My other question is if there is a possibility to allow only appointments two days in the future. Because I have to arrange something for the appointment I would like to know about an appointment two days before.
For example if today is monday than the customer can set an appointment for wednesday as the earliest date.

Best regards,
Daniel

  • Ash

    Hello @Daniel

    Welcome to WPMU community!

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

    Please use the following code to enable one day booking for an user in a week:

    // 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 7 DAY " );
    if ( $count >= 1)
    return array( 'error'=>'You have reached maximum allowed number of appointments' );
    return $reply_array;
    }
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

    You can add those codes in your functions.php in the theme, if you think your theme won’t be changed. Otherwise mu-plugins is the best solution. To use mu-plugins, go to /wp-content/ and find the folder with name 'mu-plugins'. If there is no folder in that name, then create a folder, name it 'mu-plugins', create a file inside that, give any name you like and paste the code in there. You don't need to activate that plugin. Mu-plugins means must use plugins, so it will be activated automatically always. If you use mu-plugins then add a php start tag at the beginning of the code.

    My other question is if there is a possibility to allow only appointments two days in the future.

    Please go to Appointments > Settings > General and set 48 for the option Appointments lower limit.

    Hope it helps :slight_smile: Please feel free to ask more question if you have.

    Cheers
    Ash

  • Daniel

    Hi Ash,

    you are fabulous! :slight_smile:
    The 48h offset works brilliant.

    But there is an issue with the code.
    Basically the code works.

    But this scenario should be possible:
    Now I want to add six appointments. Two appointments this week, two appointments the next week and the last two appointments the following week.
    With your code only two appointments are possible. No matter at which week I make it (I prepared the line 6 to "if ( $count >= 2)")...

    So, is there a possibility to plan the next three or four weks?

    Best regards,
    Daniel

  • Ash

    Would you please try the following code:

    // 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 YEARWEEK(START) = YEARWEEK(CURRENT_DATE)  " );
    if ( $count >= 2)
    return array( 'error'=>'You have reached maximum allowed number of appointments' );
    return $reply_array;
    }
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

    Let me know if it works.

    Cheers
    Ash

  • Hoang Ngo

    Hi @Daniel,

    I hope you are well today

    So this is the code for you :slight_smile:

    function limit_active_apps( $reply_array ) {
    	global $wpdb, $current_user, $appointments;
    
    	$start = $reply_array['start'];
    	//remove the tag
    	$ddate = preg_replace( "/<span[^>]+\>/i", "", $start );
    	$ddate = trim( str_replace( 'Date and time:', '', strip_tags( $ddate ) ) );
    	//get the number of week
    	$date     = new DateTime( $ddate );
    	$week_num = $date->format( "W" );
    	//now getting the week start and end
    	$week_start = $date->setISODate( date('Y'), $week_num )->format( 'Y-m-d' );
    	$week_end  = $date->modify('+6 days')->format('Y-m-d');
    	//build sql
    	$sql = $wpdb->prepare('SELECT COUNT(*) FROM '.$appointments->app_table.' WHERE user=%d AND (status=\'pending\' OR status=\'confirmed\' OR status=\'paid\') AND start BETWEEN %s AND %s',array(
    		$current_user->ID,
    		$week_start,
    		$week_end
    	));
    
    	$count = $wpdb->get_var($sql);
    	if ( $count >= 2 ) {
    		return array( 'error' => 'You have reached maximum allowed number of appointments' );
    	}
    
    	return $reply_array;
    }
    
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

    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.