db_manager = $db_manager; $this->settings_manager = $settings_manager; } public function export_csv() { if (!current_user_can('manage_woocommerce')) { wp_die(__('You do not have sufficient permissions to access this page.', StudiouWC_Main::get_text_domain())); } $view = isset($_POST['view']) ? sanitize_text_field($_POST['view']) : 'product_categories_summary'; $date_from = isset($_POST['date_from']) ? sanitize_text_field($_POST['date_from']) : ''; $date_to = isset($_POST['date_to']) ? sanitize_text_field($_POST['date_to']) : ''; $statuses = isset($_POST['statuses']) ? array_map('sanitize_text_field', $_POST['statuses']) : array(); if ($view === 'order_customers_list') { $report_data = $this->db_manager->get_order_customers_list($date_from, $date_to, $statuses, 999999, 1, 'customer_name', 'ASC'); $this->generate_customers_csv_output($report_data); } else { // Get all data without pagination for export $report_data = $this->db_manager->get_product_categories_summary($date_from, $date_to, $statuses, 999999, 1, 'category', 'ASC'); $settings = $this->settings_manager->get_settings(); $default_property = $settings['default_property'] ?? ''; $property_values = array(); if (!empty($default_property)) { $property_values = $this->db_manager->get_property_values($default_property); } $this->generate_csv_output($report_data, $property_values); } } private function generate_csv_output($report_data, $property_values) { // Set headers for CSV download header('Content-Type: text/csv; charset=utf-8'); $filename = sprintf( /* translators: %s: current date */ __('custom-reports-%s.csv', StudiouWC_Main::get_text_domain()), date('Y-m-d') ); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Pragma: no-cache'); header('Expires: 0'); $output = fopen('php://output', 'w'); // Add UTF-8 BOM for Excel compatibility fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF)); // CSV headers $headers = $this->get_csv_headers($property_values); fputcsv($output, $headers); // CSV data foreach ($report_data as $row) { $csv_row = $this->format_csv_row($row, $property_values); fputcsv($output, $csv_row); } fclose($output); exit; } private function generate_customers_csv_output($report_data) { header('Content-Type: text/csv; charset=utf-8'); $filename = sprintf( /* translators: %s: current date */ __('order-customers-%s.csv', StudiouWC_Main::get_text_domain()), date('Y-m-d') ); header('Content-Disposition: attachment; filename="' . $filename . '"'); header('Pragma: no-cache'); header('Expires: 0'); $output = fopen('php://output', 'w'); // Add UTF-8 BOM for Excel compatibility fprintf($output, chr(0xEF).chr(0xBB).chr(0xBF)); // CSV headers fputcsv($output, array( __('Customer Name', StudiouWC_Main::get_text_domain()), __('Email', StudiouWC_Main::get_text_domain()), __('Phone', StudiouWC_Main::get_text_domain()), __('Orders Count', StudiouWC_Main::get_text_domain()), __('Total Orders Price', StudiouWC_Main::get_text_domain()), )); // CSV data foreach ($report_data as $row) { fputcsv($output, array( $row['customer_name'], $row['customer_email'], $row['customer_phone'], $row['orders_count'], strip_tags(wc_price($row['total_price'])), )); } fclose($output); exit; } private function get_csv_headers($property_values) { $headers = array( __('Product Category', StudiouWC_Main::get_text_domain()), __('Orders Count', StudiouWC_Main::get_text_domain()) ); foreach ($property_values as $slug => $name) { /* translators: %s: property name */ $headers[] = sprintf(__('%s Count', StudiouWC_Main::get_text_domain()), $name); } $headers[] = __('Total Price', StudiouWC_Main::get_text_domain()); return $headers; } private function format_csv_row($row, $property_values) { $csv_row = array( $row['category'], $row['orders_count'] ); foreach ($property_values as $slug => $name) { $csv_row[] = $row['property_counts'][$slug] ?? 0; } // Format price without HTML for CSV $csv_row[] = strip_tags(wc_price($row['total_price'])); return $csv_row; } }