How to add conditions to appointment booking in Birchpress Scheduler

  • admin
  • Tag 1
    Tag 2
    Tag 3
    Tag 4

    Birchpress Scheduler is a plugin for WordPress, that allows you to sell services on your site. It’s a rather robust solution and while it’s very well written and modular, it’s also a terrible nightmare for a developer to extend. While the code is well thought out in OOP, it also comes with zero documentation, which means if you want to extend it, you’re in for a challenge. One of those very challenges I found myself facing, was altering it in a way that allowed my client to use it to manage services with a custom price system outside of what is provided by the plugin. Of course we had to achieve this without modifying any of the plugins core code, which meant using hooks and filters.

    After scouring their support forms and finding little help or documentation, I took a peek in the code, and resigned myself to the fact that I was in for a lot of trial and error. Our first goal was to add a conditional check to ensure users had sufficient balance to book an event. So first thing I did, was tap into the Ajax form validation filter. From what I can ascertain, this filter is triggered when the schedule appointment is triggered (eg: when a user hits submit).

    So first things first, we create out filter:

    add_filter(‘birchschedule_view_bookingform_validate_appointment_info’, ‘mo_pre_check_activity_book_in_form’);
    

    Then our function: (Note, to enforce criteria met, we do our check, and if it’s not met, we add an error code, which will display within the form, all automatically handled by Birchpresses ajax system. Within the function, you’ll have access to part of the $birchpress, and $birchschedule objects (not the entire thing from what I can tell). You also have access to the entire $_POST, which will contain all the data processed in the form. It’s safe to call WP functions and other things within this call too, since thankfully the dev’s process ajax calls properly.

    function mo_pre_check_activity_book_in_form($errors) {
    
    global $birchpress, $current_user;
    
    $vims_left = get_user_meta($current_user->ID, ‘left_vims’, true);
    
    $activity_cost = get_post_meta( $_POST[‘birs_appointment_service’], ‘_birs_service_price’,true);
    
    if ($activity_cost>$vims_left) {
    
    $errors[‘birs_appointment_service’] = __(‘Sorry, you do not have enough vims to book this appointment. ‘, ‘birchschedule’);
    
    }
    
    return $errors; // don’t forget to return the errors, even if you don’t trigger one, the form won’t process without them and will hang. This one tripped me up for a bit.
    }
    

    Now for that we can prevent users from booking without sufficient balance, we need to modify the plugin so that when a user books an appointment, it charges them accordingly. So we break out another hook:

    add_action('birchschedule_model_booking_make_appointment_after', 'mo_charge_vims_for_activity'); 
    //This will fire after an appointment is booked. Argument passed to function is $appointment_info
    

    And the function:

    	function mo_charge_vims_for_activity($appointment_info) {
    		
    		 // triggered after an appointment is booked. Checks charge, and deducts it.
    		 // Pre balance validation is done via - mo_pre_check_activity_book_in_form() to ensure user has enough of a balance to book. 
    		
    		global $current_user; 
    		
    		$vims_left = get_user_meta($current_user->ID, 'left_vims', true);
    		
    		$activity_cost = get_post_meta( $appointment_info['_birs_appointment_service'], '_birs_service_price', true); 
    		
    		if ($vims_left>0 && ($activity_cost<=$vims_left)) {
    			
    			 // we're gunna deduct vim from user. We don't want user to go negative so we check to ensure
    			 // the cost is less or equal to their balance. This isn't fool proof, but since we already validated their balance, there shouldn't be 
    			 // a reason they hit negative anyways. This is just a failsafe. 
    		
    			mo_reduce_vims($activity_cost);
    			
    		}
    		
    	} 
    

    Now every-time they book, they get deducted. But what if they cancel their appointment? Easy, we add another function and hook to process a refund like.

    Action:

    add_action ('birchschedule_model_booking_cancel_appointment_after', 'mo_refund_for_cancelling');
    

    Function:

    
    	function mo_refund_for_cancelling ($appointment_info) {
    		
    		global $current_user; 
    		
    		$vims_left = get_user_meta($current_user->ID, 'left_vims', true);
    		
    		$activity_cost = get_post_meta( $appointment_info['_birs_appointment_service'], '_birs_service_price', true); 
    		
    		mo_refund_vims($activity_cost);
    		
    	
    	}
    

    And we’re done. A lot of headache and countless hours of hair pulling later, we spend 2 minutes creating our function and achieve what we set out to accomplish. All without touching a single line of plugin code.

    Leave a comment

  • (required)