find( array( 'id' => $customer_id ) ); foreach ( $appointments as $appointment ) { $formatted_appointment = self::format_appointment( $appointment ); $appointment_time = $formatted_appointment['date'] . ' ' . $formatted_appointment['start_time']; $dt = new DateTime( $appointment_time, wp_timezone() ); $timestamp = $dt->getTimestamp(); $appointment_datetime = wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $timestamp, wp_timezone() ); $appointment_array = is_object( $appointment ) && method_exists( $appointment, 'to_array' ) ? $appointment->to_array() : (array) $appointment; $team_member_email = self::resolve_team_member_email( $formatted_appointment['team']['user_email'] ?? '', $appointment_array['team_member_id'] ?? 0 ); $notification_flow_data_for_appointment = array( 'appointment_service' => $formatted_appointment['service']['title'] ?? '', 'appointment_team_member' => $formatted_appointment['team']['display_name'] ?? '', 'start_time' => $formatted_appointment['start_time'], 'appointment_date' => $formatted_appointment['date'], 'appointment_time' => $appointment_datetime, 'attendee_count' => $formatted_appointment['attendee_count'] ?? null, 'customer_name' => $customer->name ?? '', 'user_email' => $customer->email ?? '', 'appointment_team_member_email' => $team_member_email, ); $notification_flow_data_for_appointment = apply_filters( 'booktics_appointment_scheduled_notification_flow_data', $notification_flow_data_for_appointment, $formatted_appointment ); do_action( 'booktics_gln_hook', 'appointment_scheduled', $notification_flow_data_for_appointment ); } $appointment = $appointments[0]; $formatted_appointment = self::format_appointment( $appointment ); $notification_flow_data_for_order = array( 'order_no' => $order->order_no ?? '', 'order_date' => $order->created_at ?? '', 'order_total' => $order->total ?? 0, 'customer_name' => $customer->name ?? '', 'user_email' => $customer->email ?? '', 'order_status' => $order->status ?? '', 'service_name' => $formatted_appointment['service']['title'] ?? '', 'team_name' => $formatted_appointment['team']['display_name'] ?? '', 'start_time' => $formatted_appointment['start_time'] ?? '', 'end_time' => $formatted_appointment['end_time'] ?? '', 'appointment_date' => $formatted_appointment['date'] ?? '', 'location_address' => $formatted_appointment['location']['address'] ?? '', 'appointment_status' => $formatted_appointment['status'] ?? '', 'appointment_cancel_url' => method_exists( $appointment, 'get_cancel_link' ) ? $appointment->get_cancel_link() : '', 'appointment_reschedule_url' => method_exists( $appointment, 'get_reschedule_link' ) ? $appointment->get_reschedule_link() : '', ); $notification_flow_data_for_order = apply_filters( 'booktics_order_created_notification_flow_data', $notification_flow_data_for_order, $formatted_appointment, $order ); do_action( 'booktics_gln_hook', 'order_created', $notification_flow_data_for_order ); } /** * Format a single appointment for output and notifications. * * @param object|array $appointment Appointment model or row. * * @return array */ public static function format_appointment( $appointment ): array { if ( is_object( $appointment ) && method_exists( $appointment, 'to_array' ) ) { $appointment = $appointment->to_array(); } $team_member = ( new Team_Member_Model() )->find( $appointment['team_member_id'] ); $service = ( new Service_Model() )->find( $appointment['service_id'] ); $service = $service ? $service->to_array() : array(); $additional_durations = $service['additional_durations'] ?? array(); $additional_durations = ! empty( $additional_durations ) ? array_filter( $additional_durations, function ( $duration ) use ( $appointment ) { return $duration['id'] === $appointment['additional_duration_id']; } ) : array(); $additional_duration = ! empty( $additional_durations ) ? reset( $additional_durations ) : array(); $formatted_appointment = array( 'id' => $appointment['id'] ?? 0, 'date' => $appointment['date'] ?? '', 'start_time' => $appointment['start_time'] ?? '', 'end_time' => $appointment['end_time'] ?? '', 'additional_duration_id' => $appointment['additional_duration_id'] ?? '', 'team' => array( 'id' => $team_member->id ?? null, 'display_name' => $team_member->display_name ?? '', 'image' => $team_member->image ?? '', 'user_email' => $team_member->user_email ?? '', ), 'service' => array( 'id' => $appointment['service_id'] ?? '', 'title' => $service['title'] ?? '', 'additional_duration' => $additional_duration, 'enable_google_meet' => $service['enable_google_meet'] ?? false, ), 'duration' => $appointment['duration'] ?? null, 'quantity' => $appointment['quantity'] ?? 1, 'price' => $appointment['price'] ?? 0, 'subtotal' => $appointment['subtotal'] ?? 0, 'total' => $appointment['total'] ?? 0, 'status' => $appointment['status'] ?? '', 'total_attendees' => $appointment['total_attendees'] ?? 1, 'appointment_token' => $appointment['security_token'] ?? '', 'cancel_note' => $appointment['cancel_note'] ?? '', ); return apply_filters( 'booktics_appointment_pro_format', $formatted_appointment, $appointment ); } /** * Resolve team member email with a WP user fallback. * * Model hydration may miss user_email if the team-member WP user row is * stale, and the notification SDK would then skip the team email node. * * @param string $primary Email already resolved, if any. * @param mixed $team_member_id Team member id. * * @return string */ public static function resolve_team_member_email( string $primary, $team_member_id ): string { if ( ! empty( $primary ) ) { return $primary; } $team_member_id = absint( $team_member_id ); if ( ! $team_member_id ) { return ''; } $user = get_userdata( $team_member_id ); return $user && ! empty( $user->user_email ) ? $user->user_email : ''; } }