Add filter or expand the get_all_staff_availability function

Issue #999 new
Valentina R created an issue

Requested here.

Working changed function:

public function get_all_staff_availability( $start_date, $end_date, $qty ) {
        $staff_ids       = $this->get_staff_ids();
        $available_staff = [];
        $available_qty = [];  //initializing the array to track the available quantity for each staff. we need to know the available qty for each staff to determine the maximum

        foreach ( $staff_ids as $staff_id ) {
            $availability = wc_appointments_get_total_available_appointments_for_range( $this, $start_date, $end_date, $staff_id, $qty );

            $error_string = $availability->errors['Error']['0']; //the error message that would normally be printed if this staff only was selected
            $available_appointments = substr($error_string,strpos($error_string,"of ")+3,strpos($error_string," place")-strpos($error_string,"of ")-3); //we need the qty. we don't have access to that variable here, so instead lets extract it from the error message. this is not the best way to do it, but doing it this way requires no modification to other functions. 
            $available_qty[$staff_id] = $available_appointments;  //creates an array of maximum number of places for all associated staff. later, we will use Max() to find the largest one.

            if ( $availability && ! is_wp_error( $availability ) ) {
                $available_staff[ $staff_id ] = $availability;
            }
        }

        if ( empty( $available_staff ) ) {


            return new WP_Error(
            'Error',
            sprintf(
                /* translators: %1$d: available quantity %2$s: appointment slot date %3$s: appointment slot time */
                _n( 'There is a maximum of %1$d place remaining for this slot', 'There are a maximum of %1$d places remaining on for this slot', $available_qty, 'woocommerce-appointments' ),
                max( $available_qty )
            )
            );  //this is just printing the max quantity that we found


        }

        return $available_staff;
    }

Comments (0)

  1. Log in to comment