Need help with code to extend some functionality of Appointments+

I want to stop the users in the backend to duplicate the appointments once they are confirmed.

This blocks my access to the site. Please assist.

  • Konstantinos Xenos
    • Rubber Duck Debugger

    Hey Norman ,

    I've taken a look at the code that you've provided which is working fine but you can't check for the "confirmed" state as well that way.

    We can change some pieces around and utilize the appointments_get_appointments as well to parse the existing appointments.

    So this should be working fine while checking for "confirmed" statuses as well.

    <?php
    
    add_action( 'appointments_inline_edit', 'is_worker_booked', 10, 2 );
    function is_worker_booked( $app_id, $data ) {
    	$start = $data['date'] . ' ' . $data['time'] . ':00';
    
    	$worker_args = array(
    		'worker_id'  => $data['worker'],
    		'service_id' => $data['service'],
    		// 'location_id' => $location,
    	);
    
    	$is_busy = false;
    
    	$args = array(
    		'worker'     => $data['worker'],
    		'service'    => $data['service'],
    	);
    
    	$test = appointments_get_appointments( $args );
    
    	foreach ( $test as $app ) {
    		if ( $app->start === $start && 'confirmed' === $app->status ) {
    			$is_busy = true;
    		}
    	}
    
    	if ( $is_busy ) {
    		$result = array(
    			'app_id'  => $app_id,
    			'message' => '<strong style="color:red;">The doctor is not availble on the selected time slot.</strong>',
    		);
    
    		wp_send_json( $result );
    	}
    };

    Tell me if you need any further help with this !

    Regards,
    Konstantinos

  • Norman
    • WPMU DEV Initiate

    Hey Konstantinos, the code is good, but still doesn't work for what I do. See what happen, if the appointment recognizes that there is another appointment at that time won't let it add, and it's fine, but now the problem is that I want to remove or mark an appointment as pending, for example, it won't let me do it, because still is thinking that there is an appointment at that time.

    I created one column called no-show, the staff wants to move to this list the people that didn't show up when they had an appointment. They want to keep the same time to remember what was the time the patient miss when they want to reach another day, and that's the reason now it won't let me move it.

    • Konstantinos Xenos
      • Rubber Duck Debugger

      We can add an extra check to identify if it's a "new" appointment or not if that helps. Since any "new" appointment always has an $app_id as 0 since it's not yet in the system we can make the custom check run only when that's true. This way you will still be able to edit any existing appointment but all newly created must have a free time-slot to be added:

      add_action(
      	'appointments_inline_edit',
      	function ( $app_id, $data ) {
      
      		if ( 0 === $app_id ) {
      			$start = $data['date'] . ' ' . $data['time'] . ':00';
      
      			$worker_args = array(
      				'worker_id'  => $data['worker'],
      				'service_id' => $data['service'],
      				// 'location_id' => $location,
      			);
      
      			$is_busy = false;
      
      			$args = array(
      				'worker'     => $data['worker'],
      				'service'    => $data['service'],
      			);
      
      			$test = appointments_get_appointments( $args );
      
      			foreach ( $test as $app ) {
      				if ( $app->start === $start && 'confirmed' === $app->status ) {
      					$is_busy = true;
      				}
      			}
      
      			if ( $is_busy ) {
      				$result = array(
      					'app_id'  => $app_id,
      					'message' => '<strong style="color:red;">The doctor is not availble on the selected time slot.</strong>',
      				);
      
      				wp_send_json( $result );
      			}
      		}
      	},
      	10,
      	2
      );

      Would that help getting a bit closer to the solution :slight_smile: ?

      Regards,
      Konstantinos

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.