Allow only one appointment per day

Hi,

Please can you tell me how to only allow one appointment per day per user?

Many thanks,
Ellis

    Patrick

    Hi there @Ellis

    I hope you're having a great day!

    If you are not requiring payment for appointments, and are manually confirming them, you can set the "Minimum time to pass for new appointment (secs)" to 86400. That would allow clients to only book one appointment in any 24 hour period.

    It would not, however, prevent them from waiting 24 hours to book a 2nd appointment on the same day as the 1st one. So maybe not an ideal solution.

    Perhaps my colleague @Ashok could come up with an idea for a bit of custom code to hook into an existing action or filter in the plugin.

    Ash

    Hello @Ellis

    I hope you are well today.

    First make sure you selected login required to yes, then use 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 created > DATE_SUB(CURDATE(), INTERVAL 1 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.

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

    Cheers
    Ash

    Oliver Stratton

    Update (July 2017):

    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 appointments' );
    return $reply_array;
    }
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

    (c) Majid Hamdani