Limit appointments per each service

Currently using this code from the following thread:
https://premium.wpmudev.org/forums/topic/allow-only-one-appointment-per-day#post-1264023

<?php
function limit_active_apps( $reply_array ) {
global $wpdb, $appointments;
$current_user = wp_get_current_user();
$current_user_id = $current_user->ID;
$app_table = $wpdb->prefix . 'app_appointments';
// Change statuses as required. i.e. remove pending condition if you wish
$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 1 DAY)" );

if ( $count >= 1)
return array( 'error'=>'You have reached maximum allowed number of bookings for that day' );
return $reply_array;
}
add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

Would like to modify the above on a given day, so that a user cannot make more than 1 appointment on service 1, 2 appointments on service 2 and 3 appointments on service 3

But if they make one appointment with service one, they should still be able to make 2 appointments on service 2.

  • Panos

    Hi Oliver Stratton ,

    Could you try this:

    function limit_active_apps( $reply_array ) {
    	global $wpdb, $current_user, $appointments;
    	$appointments_table = appointments_get_table( 'appointments' );
    
    	$allowed_apps_limit = array(
    		1 => 1,
    		2 => 3,
    		3 => 5,
    		4 => 1
    	);
    
    	$sent_vals = explode( ':', $_POST['value'] );
    	$location_id = (int)$sent_vals[0];
    	$service_id = (int)$sent_vals[1];
    	$provider_id = (int)$sent_vals[2];
    	$start = (int)$sent_vals[3];
    	$end = (int)$sent_vals[4];
    	$start_of_day = strtotime("midnight", $start);
    	$end_of_day   = strtotime("tomorrow", $start_of_day) - 1;
    	$start_of_day = date( 'Y-m-d H:i:s', $start_of_day );
    	$end_of_day = date( 'Y-m-d H:i:s', $end_of_day );
    
    	$service = appointments_get_service( $service_id );
    	$service_duration = $service->duration;
    
    	$count = $wpdb->get_var( "SELECT COUNT(*) FROM {$appointments_table} WHERE user={$current_user->ID} AND service={$service_id} AND status IN ( 'pending', 'confirmed', 'paid' ) AND start BETWEEN '{$start_of_day}' AND '{$end_of_day}' " );
    
    	if ( $count >= $allowed_apps_limit[$service_id] ){
    		$service_name = appointments_get_service_name( $service_id );
    		return array( 'error'=>'You have reached maximum allowed daily number of appointments for "' . $service_name .'"' );
    	}
    
    	return $reply_array;
    }
    
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

    You'll need to set the number of allowed appointments per service. This can be done in the array:

    $allowed_apps_limit = array(
    		1 => 1,
    		2 => 3,
    		3 => 5,
    		4 => 1
    	);

    Modify it as required to suit your needs. the first number in each row is the service id, the second one is the allowed numbers.

    You mentioned

    on a given day

    So if the limit is exceeded in one day, it could be available in the next day.

    Hope this helps

    Cheers!