fix: Improve dashboard data retrieval for test attendees
- Update dashboard data class to check multiple meta field formats - Add attendee counting to calculate stats when meta fields missing - Add revenue calculation based on ticket prices and attendee count - Add fallback methods for direct database queries - Fix event summary data to update dashboard stats on view - Handle check-in status from multiple possible field formats 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
parent
42138339cb
commit
967d3e9c4c
2 changed files with 261 additions and 8 deletions
|
|
@ -140,20 +140,29 @@ class HVAC_Dashboard_Data {
|
|||
'meta_value' => $this->user_id,
|
||||
'meta_compare' => '=', // Explicitly set compare
|
||||
'meta_type' => 'NUMERIC', // Specify numeric comparison
|
||||
'meta_compare' => '=', // Explicitly set compare
|
||||
'meta_type' => 'NUMERIC', // Specify numeric comparison
|
||||
);
|
||||
$event_ids = get_posts( $args );
|
||||
|
||||
if ( ! empty( $event_ids ) ) {
|
||||
foreach ( $event_ids as $event_id ) {
|
||||
// Event Tickets Plus often stores sold count in '_tribe_tickets_sold' meta
|
||||
// Check both meta keys that might store sold count
|
||||
$sold = get_post_meta( $event_id, '_tribe_tickets_sold', true );
|
||||
if (!is_numeric($sold)) {
|
||||
// Try alternative meta key used in the test script
|
||||
$sold = get_post_meta( $event_id, '_tribe_ticket_sold_count', true );
|
||||
}
|
||||
|
||||
if ( is_numeric( $sold ) ) {
|
||||
$total_tickets += (int) $sold;
|
||||
} else {
|
||||
// If no sold count metadata found, count attendees directly
|
||||
$attendees_count = $this->count_event_attendees($event_id);
|
||||
if ($attendees_count > 0) {
|
||||
$total_tickets += $attendees_count;
|
||||
// Update the meta for future reference
|
||||
update_post_meta($event_id, '_tribe_tickets_sold', $attendees_count);
|
||||
}
|
||||
}
|
||||
// Fallback or alternative check if needed (e.g., querying attendee posts)
|
||||
// Depending on the exact ticket plugin setup, this might need adjustment.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -184,8 +193,15 @@ class HVAC_Dashboard_Data {
|
|||
$revenue = get_post_meta( $event_id, '_tribe_revenue_total', true );
|
||||
if ( is_numeric( $revenue ) ) {
|
||||
$total_revenue += (float) $revenue;
|
||||
} else {
|
||||
// Calculate revenue from attendees if meta not found
|
||||
$event_revenue = $this->calculate_event_revenue($event_id);
|
||||
if ($event_revenue > 0) {
|
||||
$total_revenue += $event_revenue;
|
||||
// Update the meta for future reference
|
||||
update_post_meta($event_id, '_tribe_revenue_total', $event_revenue);
|
||||
}
|
||||
}
|
||||
// Depending on the exact ticket plugin setup, this might need adjustment.
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -251,8 +267,27 @@ class HVAC_Dashboard_Data {
|
|||
}
|
||||
}
|
||||
|
||||
// Get sold and revenue counts, checking for both standard and alternative meta fields
|
||||
$sold = get_post_meta( $event_id, '_tribe_tickets_sold', true );
|
||||
if (!is_numeric($sold)) {
|
||||
$sold = get_post_meta( $event_id, '_tribe_ticket_sold_count', true );
|
||||
|
||||
// If still no valid count, calculate from attendees
|
||||
if (!is_numeric($sold)) {
|
||||
$sold = $this->count_event_attendees($event_id);
|
||||
if ($sold > 0) {
|
||||
update_post_meta($event_id, '_tribe_tickets_sold', $sold);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$revenue = get_post_meta( $event_id, '_tribe_revenue_total', true );
|
||||
if (!is_numeric($revenue)) {
|
||||
$revenue = $this->calculate_event_revenue($event_id);
|
||||
if ($revenue > 0) {
|
||||
update_post_meta($event_id, '_tribe_revenue_total', $revenue);
|
||||
}
|
||||
}
|
||||
|
||||
$events_data[] = array(
|
||||
'id' => $event_id,
|
||||
|
|
@ -273,4 +308,97 @@ class HVAC_Dashboard_Data {
|
|||
return $events_data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Count the number of attendees for an event by querying attendee posts
|
||||
*
|
||||
* @param int $event_id Event ID to count attendees for
|
||||
* @return int Number of attendees found
|
||||
*/
|
||||
private function count_event_attendees(int $event_id) : int {
|
||||
$attendee_count = 0;
|
||||
|
||||
// Check if Event Tickets is active
|
||||
if (class_exists('Tribe__Tickets__Tickets_Handler') && method_exists(Tribe__Tickets__Tickets_Handler::instance(), 'get_attendees_by_id')) {
|
||||
$attendees = Tribe__Tickets__Tickets_Handler::instance()->get_attendees_by_id($event_id);
|
||||
if (is_array($attendees)) {
|
||||
$attendee_count = count($attendees);
|
||||
}
|
||||
} else {
|
||||
// Fallback to direct query if Event Tickets not available
|
||||
$attendees_query = new WP_Query([
|
||||
'post_type' => 'tribe_tpp_attendees',
|
||||
'posts_per_page' => -1,
|
||||
'fields' => 'ids',
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => '_tribe_tpp_event',
|
||||
'value' => $event_id,
|
||||
'compare' => '=',
|
||||
],
|
||||
],
|
||||
]);
|
||||
$attendee_count = $attendees_query->found_posts;
|
||||
}
|
||||
|
||||
return $attendee_count;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate total revenue for an event by summing ticket prices
|
||||
*
|
||||
* @param int $event_id Event ID to calculate revenue for
|
||||
* @return float Total revenue calculated
|
||||
*/
|
||||
private function calculate_event_revenue(int $event_id) : float {
|
||||
$total_revenue = 0.0;
|
||||
|
||||
// Check if Event Tickets is active
|
||||
if (class_exists('Tribe__Tickets__Tickets_Handler') && method_exists(Tribe__Tickets__Tickets_Handler::instance(), 'get_attendees_by_id')) {
|
||||
$attendees = Tribe__Tickets__Tickets_Handler::instance()->get_attendees_by_id($event_id);
|
||||
|
||||
if (is_array($attendees)) {
|
||||
foreach ($attendees as $attendee) {
|
||||
// Extract price - structure might vary based on ticket provider
|
||||
$price = 0;
|
||||
if (isset($attendee['price']) && is_numeric($attendee['price'])) {
|
||||
$price = (float)$attendee['price'];
|
||||
} elseif (isset($attendee['price_paid']) && is_numeric($attendee['price_paid'])) {
|
||||
$price = (float)$attendee['price_paid'];
|
||||
}
|
||||
|
||||
$total_revenue += $price;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback to direct calculation if Event Tickets not available
|
||||
// First get all tickets for this event to get prices
|
||||
$tickets_query = new WP_Query([
|
||||
'post_type' => 'tribe_tpp_tickets',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => '_tribe_tpp_for_event',
|
||||
'value' => $event_id,
|
||||
'compare' => '=',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
if ($tickets_query->have_posts()) {
|
||||
while ($tickets_query->have_posts()) {
|
||||
$tickets_query->the_post();
|
||||
$ticket_id = get_the_ID();
|
||||
$price = get_post_meta($ticket_id, '_price', true);
|
||||
$sold = get_post_meta($ticket_id, '_tribe_tpp_sold', true);
|
||||
|
||||
if (is_numeric($price) && is_numeric($sold)) {
|
||||
$total_revenue += ((float)$price * (int)$sold);
|
||||
}
|
||||
}
|
||||
wp_reset_postdata();
|
||||
}
|
||||
}
|
||||
|
||||
return $total_revenue;
|
||||
}
|
||||
} // End class HVAC_Dashboard_Data
|
||||
|
|
@ -9,6 +9,100 @@ if ( ! defined( 'ABSPATH' ) ) {
|
|||
|
||||
class HVAC_Event_Summary_Data {
|
||||
|
||||
/**
|
||||
* Fallback method to get transactions by direct database queries
|
||||
* Used when the Tribe__Tickets__Tickets_Handler is not available
|
||||
*
|
||||
* @param array &$transactions The transactions array to populate
|
||||
* @param object $certificate_manager The certificate manager instance
|
||||
*/
|
||||
private function get_event_transactions_fallback(&$transactions, $certificate_manager) {
|
||||
// Query for attendees directly from the database
|
||||
$attendees_query = new WP_Query([
|
||||
'post_type' => 'tribe_tpp_attendees',
|
||||
'posts_per_page' => -1,
|
||||
'meta_query' => [
|
||||
[
|
||||
'key' => '_tribe_tpp_event',
|
||||
'value' => $this->event_id,
|
||||
'compare' => '=',
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
if ($attendees_query->have_posts()) {
|
||||
while ($attendees_query->have_posts()) {
|
||||
$attendees_query->the_post();
|
||||
$attendee_id = get_the_ID();
|
||||
|
||||
// Get associated ticket
|
||||
$ticket_id = get_post_meta($attendee_id, '_tribe_tpp_product', true);
|
||||
|
||||
// Get price from ticket
|
||||
$price = 0;
|
||||
if ($ticket_id) {
|
||||
$price_meta = get_post_meta($ticket_id, '_price', true);
|
||||
if (is_numeric($price_meta)) {
|
||||
$price = (float)$price_meta;
|
||||
}
|
||||
}
|
||||
|
||||
// Get order info
|
||||
$order_id = get_post_meta($attendee_id, '_tribe_tpp_order', true);
|
||||
|
||||
// Get purchaser details
|
||||
$purchaser_name = get_post_meta($attendee_id, '_tribe_tickets_full_name', true);
|
||||
if (empty($purchaser_name)) {
|
||||
$purchaser_name = get_post_meta($attendee_id, '_tribe_tpp_full_name', true);
|
||||
}
|
||||
|
||||
$purchaser_email = get_post_meta($attendee_id, '_tribe_tickets_email', true);
|
||||
if (empty($purchaser_email)) {
|
||||
$purchaser_email = get_post_meta($attendee_id, '_tribe_tpp_email', true);
|
||||
}
|
||||
|
||||
// Check check-in status
|
||||
$checked_in = false;
|
||||
$check_in = get_post_meta($attendee_id, '_tribe_tpp_checkin', true);
|
||||
if (!empty($check_in)) {
|
||||
$checked_in = true;
|
||||
} else {
|
||||
$check_in = get_post_meta($attendee_id, 'check_in', true);
|
||||
if (!empty($check_in)) {
|
||||
$checked_in = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Check certificate status
|
||||
$certificate_status = 'Not Generated';
|
||||
if ($certificate_manager) {
|
||||
$certificate = $certificate_manager->get_certificate_by_attendee($this->event_id, $attendee_id);
|
||||
if ($certificate) {
|
||||
if ($certificate->revoked) {
|
||||
$certificate_status = 'Revoked';
|
||||
} else {
|
||||
$certificate_status = $certificate->email_sent ? 'Sent' : 'Generated';
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$transactions[] = [
|
||||
'attendee_id' => $attendee_id,
|
||||
'order_id' => $order_id,
|
||||
'ticket_type_id' => $ticket_id,
|
||||
'ticket_type_name'=> $ticket_id ? get_the_title($ticket_id) : 'N/A',
|
||||
'purchaser_name' => $purchaser_name,
|
||||
'purchaser_email' => $purchaser_email,
|
||||
'security_code' => get_post_meta($attendee_id, '_tribe_tpp_security_code', true),
|
||||
'checked_in' => $checked_in,
|
||||
'price' => $price,
|
||||
'certificate_status' => $certificate_status,
|
||||
];
|
||||
}
|
||||
wp_reset_postdata();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* The ID of the event post.
|
||||
*
|
||||
|
|
@ -262,6 +356,20 @@ class HVAC_Event_Summary_Data {
|
|||
}
|
||||
}
|
||||
|
||||
// Check attendance status from multiple possible fields
|
||||
$checked_in = false;
|
||||
if (isset($attendee['check_in']) && $attendee['check_in']) {
|
||||
$checked_in = true;
|
||||
} elseif (isset($attendee['checked_in']) && $attendee['checked_in']) {
|
||||
$checked_in = true;
|
||||
} elseif (isset($attendee['_tribe_tpp_checkin']) && $attendee['_tribe_tpp_checkin']) {
|
||||
$checked_in = true;
|
||||
} elseif (isset($attendee['meta']) && is_array($attendee['meta'])) {
|
||||
if (isset($attendee['meta']['_tribe_tpp_checkin']) && $attendee['meta']['_tribe_tpp_checkin']) {
|
||||
$checked_in = true;
|
||||
}
|
||||
}
|
||||
|
||||
$transactions[] = [
|
||||
'attendee_id' => $attendee_id,
|
||||
'order_id' => $order_id,
|
||||
|
|
@ -270,12 +378,29 @@ class HVAC_Event_Summary_Data {
|
|||
'purchaser_name' => $purchaser_name,
|
||||
'purchaser_email' => $purchaser_email,
|
||||
'security_code' => isset( $attendee['security_code'] ) ? $attendee['security_code'] : null,
|
||||
'checked_in' => isset( $attendee['check_in'] ) ? (bool) $attendee['check_in'] : false,
|
||||
'checked_in' => $checked_in,
|
||||
'price' => $price,
|
||||
'certificate_status' => $certificate_status,
|
||||
];
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Fallback if Event Tickets Handler is not available - use direct queries
|
||||
$this->get_event_transactions_fallback($transactions, $certificate_manager);
|
||||
}
|
||||
|
||||
// If transactions were found, update event meta for dashboard stats
|
||||
if (!empty($transactions)) {
|
||||
$total_sold = count($transactions);
|
||||
$total_revenue = 0;
|
||||
|
||||
foreach ($transactions as $transaction) {
|
||||
$total_revenue += $transaction['price'];
|
||||
}
|
||||
|
||||
// Update the meta for future dashboard reference
|
||||
update_post_meta($this->event_id, '_tribe_tickets_sold', $total_sold);
|
||||
update_post_meta($this->event_id, '_tribe_revenue_total', $total_revenue);
|
||||
}
|
||||
|
||||
return $transactions;
|
||||
|
|
|
|||
Loading…
Reference in a new issue