fix: Update Admin Dashboard metrics for attendees and revenue
- Fix attendee count to properly query Event Tickets data - Add support for counting attendees across all ticket providers (RSVP, PayPal, Tickets Commerce) - Implement detailed revenue metrics from multiple sources - Support Event Tickets Plus with WooCommerce ticket sales - Add support for Tickets Commerce revenue statistics - Add support for legacy PayPal ticket sales - Improve dashboard accuracy with proper database queries 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
		
							parent
							
								
									54312badba
								
							
						
					
					
						commit
						6974385990
					
				
					 1 changed files with 201 additions and 36 deletions
				
			
		|  | @ -396,13 +396,51 @@ class HVAC_Admin_Dashboard { | ||||||
|             AND pm.meta_value = '1' |             AND pm.meta_value = '1' | ||||||
|         ", 'tribe_events'));
 |         ", 'tribe_events'));
 | ||||||
|          |          | ||||||
|         // Total attendees (would require integration with Event Tickets Plus)
 |         // Total attendees using Event Tickets data
 | ||||||
|         $total_attendees = 0; |         $total_attendees = 0; | ||||||
|         if (class_exists('Tribe__Tickets_Plus__Main')) { |          | ||||||
|             $total_attendees = $wpdb->get_var(" |         // Check if Event Tickets is active
 | ||||||
|                 SELECT COUNT(DISTINCT attendee_id)  |         if (class_exists('Tribe__Tickets__Main')) { | ||||||
|                 FROM {$wpdb->prefix}tickets_attendees |             // Get all attendee post types from Event Tickets
 | ||||||
|  |             $attendee_types = [ | ||||||
|  |                 'tribe_rsvp_attendees',        // RSVP attendees
 | ||||||
|  |                 'tribe_tpp_attendees',         // PayPal attendees
 | ||||||
|  |                 'tec_tc_attendee'              // Tickets Commerce attendees
 | ||||||
|  |             ]; | ||||||
|  |              | ||||||
|  |             // Preparing for the SQL query
 | ||||||
|  |             $types_placeholder = implode(', ', array_fill(0, count($attendee_types), '%s')); | ||||||
|  |             $query_args = $attendee_types; | ||||||
|  |              | ||||||
|  |             // Add status condition - Public order statuses 
 | ||||||
|  |             // (based on Tribe__Tickets__Attendee_Repository class)
 | ||||||
|  |             $public_order_statuses = [ | ||||||
|  |                 'yes',           // RSVP
 | ||||||
|  |                 'completed',     // PayPal Legacy
 | ||||||
|  |                 'wc-completed',  // WooCommerce
 | ||||||
|  |                 'publish',       // Easy Digital Downloads, Legacy
 | ||||||
|  |                 'complete',      // Easy Digital Downloads
 | ||||||
|  |             ]; | ||||||
|  |              | ||||||
|  |             // Count attendees with proper status
 | ||||||
|  |             foreach ($attendee_types as $post_type) { | ||||||
|  |                 $count = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                     SELECT COUNT(*) FROM {$wpdb->posts}  | ||||||
|  |                     WHERE post_type = %s  | ||||||
|  |                     AND post_status = 'publish' | ||||||
|  |                 ", $post_type));
 | ||||||
|  |                  | ||||||
|  |                 $total_attendees += (int)$count; | ||||||
|  |             } | ||||||
|  |              | ||||||
|  |             // If WooCommerce Tickets is active, count WooCommerce ticket attendees too
 | ||||||
|  |             if (class_exists('Tribe__Tickets_Plus__Commerce__WooCommerce__Main')) { | ||||||
|  |                 $wc_attendees = $wpdb->get_var(" | ||||||
|  |                     SELECT COUNT(*) FROM {$wpdb->postmeta}  | ||||||
|  |                     WHERE meta_key = '_tribe_wooticket_attendance' | ||||||
|                 ");
 |                 ");
 | ||||||
|  |                 $total_attendees += (int)$wc_attendees; | ||||||
|  |             } | ||||||
|         } |         } | ||||||
|          |          | ||||||
|         return array( |         return array( | ||||||
|  | @ -423,22 +461,42 @@ class HVAC_Admin_Dashboard { | ||||||
|          |          | ||||||
|         $week_ago = date('Y-m-d H:i:s', strtotime('-1 week')); |         $week_ago = date('Y-m-d H:i:s', strtotime('-1 week')); | ||||||
|          |          | ||||||
|         // These would need to be integrated with your payment system
 |  | ||||||
|         // Using placeholder values for demonstration
 |  | ||||||
|         $total_revenue = 0; |         $total_revenue = 0; | ||||||
|         $revenue_week = 0; |         $revenue_week = 0; | ||||||
|         $total_purchases = 0; |         $total_purchases = 0; | ||||||
|         $purchases_week = 0; |         $purchases_week = 0; | ||||||
|          |          | ||||||
|         // If using WooCommerce for tickets
 |         // If using Event Tickets Plus with WooCommerce
 | ||||||
|         if (class_exists('WooCommerce')) { |         if (class_exists('Tribe__Tickets_Plus__Commerce__WooCommerce__Main')) { | ||||||
|             // Total revenue
 |             // Gather data from WooCommerce orders that contain tickets
 | ||||||
|  |             // First, find all orders with ticket items
 | ||||||
|  |             $ticket_product_ids = $wpdb->get_col(" | ||||||
|  |                 SELECT ID FROM {$wpdb->posts}  | ||||||
|  |                 WHERE post_type = 'tribe_wooticket' | ||||||
|  |             ");
 | ||||||
|  |              | ||||||
|  |             if (!empty($ticket_product_ids)) { | ||||||
|  |                 $ticket_product_ids_str = implode(',', array_map('intval', $ticket_product_ids)); | ||||||
|  |                  | ||||||
|  |                 // Find orders that contain ticket products
 | ||||||
|  |                 $ticket_order_ids = $wpdb->get_col(" | ||||||
|  |                     SELECT order_id  | ||||||
|  |                     FROM {$wpdb->prefix}woocommerce_order_items oi | ||||||
|  |                     JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim ON oi.order_item_id = oim.order_item_id | ||||||
|  |                     WHERE oim.meta_key = '_product_id' | ||||||
|  |                     AND oim.meta_value IN ({$ticket_product_ids_str}) | ||||||
|  |                 ");
 | ||||||
|  |                  | ||||||
|  |                 if (!empty($ticket_order_ids)) { | ||||||
|  |                     $ticket_order_ids_str = implode(',', array_map('intval', $ticket_order_ids)); | ||||||
|  |                      | ||||||
|  |                     // Total revenue from these orders
 | ||||||
|                     $total_revenue = $wpdb->get_var(" |                     $total_revenue = $wpdb->get_var(" | ||||||
|                         SELECT SUM(meta.meta_value)  |                         SELECT SUM(meta.meta_value)  | ||||||
|                         FROM {$wpdb->postmeta} meta |                         FROM {$wpdb->postmeta} meta | ||||||
|                         JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID |                         JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID | ||||||
|                         WHERE meta.meta_key = '_order_total' |                         WHERE meta.meta_key = '_order_total' | ||||||
|                 AND posts.post_type = 'shop_order' |                         AND posts.ID IN ({$ticket_order_ids_str}) | ||||||
|                         AND posts.post_status IN ('wc-completed', 'wc-processing') |                         AND posts.post_status IN ('wc-completed', 'wc-processing') | ||||||
|                     ");
 |                     ");
 | ||||||
|                      |                      | ||||||
|  | @ -448,34 +506,141 @@ class HVAC_Admin_Dashboard { | ||||||
|                         FROM {$wpdb->postmeta} meta |                         FROM {$wpdb->postmeta} meta | ||||||
|                         JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID |                         JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID | ||||||
|                         WHERE meta.meta_key = '_order_total' |                         WHERE meta.meta_key = '_order_total' | ||||||
|                 AND posts.post_type = 'shop_order' |                         AND posts.ID IN ({$ticket_order_ids_str}) | ||||||
|                         AND posts.post_status IN ('wc-completed', 'wc-processing') |                         AND posts.post_status IN ('wc-completed', 'wc-processing') | ||||||
|                         AND posts.post_date >= %s |                         AND posts.post_date >= %s | ||||||
|                     ", $week_ago));
 |                     ", $week_ago));
 | ||||||
|                      |                      | ||||||
|             // Total purchases
 |                     // Total purchases (count of orders)
 | ||||||
|             $total_purchases = $wpdb->get_var(" |                     $total_purchases = count($ticket_order_ids); | ||||||
|                 SELECT COUNT(*)  |  | ||||||
|                 FROM {$wpdb->posts} |  | ||||||
|                 WHERE post_type = 'shop_order' |  | ||||||
|                 AND post_status IN ('wc-completed', 'wc-processing') |  | ||||||
|             ");
 |  | ||||||
|                      |                      | ||||||
|                     // Purchases this week
 |                     // Purchases this week
 | ||||||
|                     $purchases_week = $wpdb->get_var($wpdb->prepare(" |                     $purchases_week = $wpdb->get_var($wpdb->prepare(" | ||||||
|                         SELECT COUNT(*)  |                         SELECT COUNT(*)  | ||||||
|                         FROM {$wpdb->posts} |                         FROM {$wpdb->posts} | ||||||
|                 WHERE post_type = 'shop_order' |                         WHERE ID IN ({$ticket_order_ids_str}) | ||||||
|                         AND post_status IN ('wc-completed', 'wc-processing') |                         AND post_status IN ('wc-completed', 'wc-processing') | ||||||
|                         AND post_date >= %s |                         AND post_date >= %s | ||||||
|                     ", $week_ago));
 |                     ", $week_ago));
 | ||||||
|                 } |                 } | ||||||
|  |             } | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         // Check for Tickets Commerce data (modern Event Tickets)
 | ||||||
|  |         if (class_exists('TEC\\Tickets\\Commerce\\Order')) { | ||||||
|  |             // Get orders through Tickets Commerce
 | ||||||
|  |             $tc_order_post_type = \TEC\Tickets\Commerce\Order::POSTTYPE; | ||||||
|  |              | ||||||
|  |             $tc_completed_statuses = [ | ||||||
|  |                 'completed', 'pfc-completed', 'tpay-completed', 'paid' | ||||||
|  |             ]; | ||||||
|  |              | ||||||
|  |             $placeholders = implode(', ', array_fill(0, count($tc_completed_statuses), '%s')); | ||||||
|  |             $query_args = $tc_completed_statuses; | ||||||
|  |              | ||||||
|  |             // Calculate total revenue
 | ||||||
|  |             $tc_total_revenue = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT SUM(meta.meta_value)  | ||||||
|  |                 FROM {$wpdb->postmeta} meta | ||||||
|  |                 JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID | ||||||
|  |                 WHERE meta.meta_key = '_tec_tc_order_total' | ||||||
|  |                 AND posts.post_type = %s | ||||||
|  |                 AND posts.post_status IN ($placeholders) | ||||||
|  |             ", array_merge([$tc_order_post_type], $query_args)));
 | ||||||
|  |              | ||||||
|  |             // Calculate this week's revenue
 | ||||||
|  |             $query_args[] = $week_ago; | ||||||
|  |             $tc_revenue_week = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT SUM(meta.meta_value)  | ||||||
|  |                 FROM {$wpdb->postmeta} meta | ||||||
|  |                 JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID | ||||||
|  |                 WHERE meta.meta_key = '_tec_tc_order_total' | ||||||
|  |                 AND posts.post_type = %s | ||||||
|  |                 AND posts.post_status IN ($placeholders) | ||||||
|  |                 AND posts.post_date >= %s | ||||||
|  |             ", array_merge([$tc_order_post_type], $query_args)));
 | ||||||
|  |              | ||||||
|  |             // Count total purchases
 | ||||||
|  |             $tc_total_purchases = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT COUNT(*)  | ||||||
|  |                 FROM {$wpdb->posts} | ||||||
|  |                 WHERE post_type = %s | ||||||
|  |                 AND post_status IN ($placeholders) | ||||||
|  |             ", array_merge([$tc_order_post_type], $tc_completed_statuses)));
 | ||||||
|  |              | ||||||
|  |             // Count purchases this week
 | ||||||
|  |             $tc_purchases_week = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT COUNT(*)  | ||||||
|  |                 FROM {$wpdb->posts} | ||||||
|  |                 WHERE post_type = %s | ||||||
|  |                 AND post_status IN ($placeholders) | ||||||
|  |                 AND post_date >= %s | ||||||
|  |             ", array_merge([$tc_order_post_type], $tc_completed_statuses, [$week_ago])));
 | ||||||
|  |              | ||||||
|  |             // Add TC values to totals
 | ||||||
|  |             $total_revenue += (float)$tc_total_revenue; | ||||||
|  |             $revenue_week += (float)$tc_revenue_week; | ||||||
|  |             $total_purchases += (int)$tc_total_purchases; | ||||||
|  |             $purchases_week += (int)$tc_purchases_week; | ||||||
|  |         } | ||||||
|  |          | ||||||
|  |         // Tribe Commerce PayPal (legacy from Event Tickets)
 | ||||||
|  |         if (class_exists('Tribe__Tickets__Commerce__PayPal__Main')) { | ||||||
|  |             // PayPal orders are stored as posts with meta data
 | ||||||
|  |             $pp_completed_status = 'completed'; | ||||||
|  |              | ||||||
|  |             // Calculate total revenue
 | ||||||
|  |             $pp_total_revenue = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT SUM(meta.meta_value)  | ||||||
|  |                 FROM {$wpdb->postmeta} meta | ||||||
|  |                 JOIN {$wpdb->postmeta} status ON status.post_id = meta.post_id | ||||||
|  |                 WHERE meta.meta_key = '_tribe_tpp_gross' | ||||||
|  |                 AND status.meta_key = '_tribe_tpp_status' | ||||||
|  |                 AND status.meta_value = %s | ||||||
|  |             ", $pp_completed_status));
 | ||||||
|  |              | ||||||
|  |             // Calculate this week's revenue
 | ||||||
|  |             $pp_revenue_week = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT SUM(meta.meta_value)  | ||||||
|  |                 FROM {$wpdb->postmeta} meta | ||||||
|  |                 JOIN {$wpdb->postmeta} status ON status.post_id = meta.post_id | ||||||
|  |                 JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID | ||||||
|  |                 WHERE meta.meta_key = '_tribe_tpp_gross' | ||||||
|  |                 AND status.meta_key = '_tribe_tpp_status' | ||||||
|  |                 AND status.meta_value = %s | ||||||
|  |                 AND posts.post_date >= %s | ||||||
|  |             ", $pp_completed_status, $week_ago));
 | ||||||
|  |              | ||||||
|  |             // Count total PayPal orders
 | ||||||
|  |             $pp_total_purchases = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT COUNT(DISTINCT post_id)  | ||||||
|  |                 FROM {$wpdb->postmeta} | ||||||
|  |                 WHERE meta_key = '_tribe_tpp_status' | ||||||
|  |                 AND meta_value = %s | ||||||
|  |             ", $pp_completed_status));
 | ||||||
|  |              | ||||||
|  |             // Count purchases this week
 | ||||||
|  |             $pp_purchases_week = $wpdb->get_var($wpdb->prepare(" | ||||||
|  |                 SELECT COUNT(DISTINCT meta.post_id)  | ||||||
|  |                 FROM {$wpdb->postmeta} meta | ||||||
|  |                 JOIN {$wpdb->posts} posts ON meta.post_id = posts.ID | ||||||
|  |                 WHERE meta.meta_key = '_tribe_tpp_status' | ||||||
|  |                 AND meta.meta_value = %s | ||||||
|  |                 AND posts.post_date >= %s | ||||||
|  |             ", $pp_completed_status, $week_ago));
 | ||||||
|  |              | ||||||
|  |             // Add PayPal values to totals
 | ||||||
|  |             $total_revenue += (float)$pp_total_revenue; | ||||||
|  |             $revenue_week += (float)$pp_revenue_week; | ||||||
|  |             $total_purchases += (int)$pp_total_purchases; | ||||||
|  |             $purchases_week += (int)$pp_purchases_week; | ||||||
|  |         } | ||||||
|          |          | ||||||
|         return array( |         return array( | ||||||
|             'total_revenue' => $total_revenue ?: 0, |             'total_revenue' => $total_revenue ?: 0, | ||||||
|             'revenue_week' => $revenue_week ?: 0, |             'revenue_week' => $revenue_week ?: 0, | ||||||
|             'total_purchases' => $total_purchases, |             'total_purchases' => $total_purchases ?: 0, | ||||||
|             'purchases_week' => $purchases_week |             'purchases_week' => $purchases_week ?: 0 | ||||||
|         ); |         ); | ||||||
|     } |     } | ||||||
|      |      | ||||||
|  |  | ||||||
		Loading…
	
		Reference in a new issue