| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- <?php
- // Handles custom bulk actions for orders.
- // visualized
- class Bulk_Actions_Manager {
- public function __construct() {
- add_filter('bulk_actions-woocommerce_page_wc-orders', array($this, 'register_bulk_actions'));
- add_filter('handle_bulk_actions-woocommerce_page_wc-orders', array($this, 'handle_bulk_actions'), 10, 3);
- }
- public function register_bulk_actions($bulk_actions) {
- $bulk_actions['prepare_to_printing_export'] = __('Prepare to Printing Export', 'studiou-wc-ord-print-statuses');
- $bulk_actions['set_status_to_prepare_to_printing'] = __('Set Status to Prepare to Printing', 'studiou-wc-ord-print-statuses');
- $bulk_actions['set_status_to_in_printing'] = __('Set Status to In Printing', 'studiou-wc-ord-print-statuses');
- UtilsLog::log('Bulk actions registered');
- return $bulk_actions;
- }
- public function handle_bulk_actions($redirect_to, $action, $post_ids) {
- switch ($action) {
- case 'prepare_to_printing_export':
- $redirect_to = $this->prepare_to_printing_export($post_ids, $redirect_to);
- break;
- case 'set_status_to_prepare_to_printing':
- $redirect_to = $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
- break;
- case 'set_status_to_in_printing':
- $redirect_to = $this->set_status_to_in_printing($post_ids, $redirect_to);
- break;
- }
- return $redirect_to;
- }
- private function prepare_to_printing_export($post_ids, $redirect_to) {
- // Get orders data using the DB Manager
- $orders_data = Studiou_DB_Manager::get_orders_for_printing($post_ids);
-
- // Update order statuses
- $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
-
- UtilsLog::log('Order statuses set to "to-print" after export.');
- UtilsLog::message('Order statuses (' . count($orders_data) . ') set to "to-print" after export.', 'info' );
- if (!empty($orders_data)) {
- $csv_data = $this->generate_csv($orders_data);
- UtilsLog::log('file prepare_to_printing_export.csv exported');
- $this->output_csv($csv_data, 'prepare_to_printing_export.csv');
- }
-
- return $redirect_to;
- }
- private function set_status_to_prepare_to_printing($post_ids, $redirect_to) {
- // Use the DB Manager to update order statuses
- Studiou_DB_Manager::set_orders_to_prepare_printing($post_ids);
-
- $redirect_to = add_query_arg('bulk_action', 'marked_prepare_to_printing', $redirect_to);
- return $redirect_to;
- }
- private function set_status_to_in_printing($post_ids, $redirect_to) {
- // Use the DB Manager to update order statuses
- Studiou_DB_Manager::set_orders_to_in_printing($post_ids);
-
- $redirect_to = add_query_arg('bulk_action', 'marked_in_printing', $redirect_to);
- return $redirect_to;
- }
- private function generate_csv($data) {
- $output = fopen('php://temp', 'w');
- fputcsv($output, array_keys((array)$data[0]),','); // Headers
- foreach ($data as $row) {
- fputcsv($output, (array)$row, ',');
- }
- rewind($output);
- $csv = stream_get_contents($output);
- fclose($output);
- return $csv;
- }
- private function output_csv($csv_data, $filename) {
- header('Content-Type: text/csv');
- header('Content-Disposition: attachment; filename="' . $filename . '"');
- echo $csv_data;
- exit;
- }
- }
|