Limit appointments per user per months

I've tried the following code but it didn't work and couldn't find any update on this one.
This is were the code was found https://premium.wpmudev.org/forums/topic/limiting-the-number-of-appointments-per-a-month-per-user#post-604183

Here's the code I tested:

// 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 ( $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' );

I tested it with the required login option.

Could anyone help me to figure this one :slight_smile:

Thanks in advance!

  • Majid

    Hello GUSTAVO
    I hope you are doing great today :slight_smile:

    The code logic is working fine but some changes were made to the plugin core affected the $appointments->app_table value. Try using this snippet instead, you can change the maximum number of appointments per month by changing this variable value
    $max_apps = 1;
    In my case a user can only make one appointment per month.

    function limit_active_apps( $reply_array ) {
    	global $wpdb, $current_user, $appointments;
    
    	$max_apps = 1;
    	$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 start > NOW() - INTERVAL 30 DAY " );
    	if ( $count >= $max_apps )
    	return array( 'error'=>'You have reached maximum allowed number of appointments' );
    	return $reply_array;
    }
    add_filter( 'app_pre_confirmation_reply', 'limit_active_apps' );

    I hope this helps :slight_smile:
    Cheers,
    Majid