class-bulk-actions-manager.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. // Handles custom bulk actions for orders.
  3. // visualized
  4. class Bulk_Actions_Manager {
  5. public function __construct() {
  6. add_filter('bulk_actions-woocommerce_page_wc-orders', array($this, 'register_bulk_actions'));
  7. add_filter('handle_bulk_actions-woocommerce_page_wc-orders', array($this, 'handle_bulk_actions'), 10, 3);
  8. }
  9. public function register_bulk_actions($bulk_actions) {
  10. $bulk_actions['prepare_to_printing_export'] = __('Prepare to Printing Export', 'studiou-wc-ord-print-statuses');
  11. $bulk_actions['set_status_to_prepare_to_printing'] = __('Set Status to Prepare to Printing', 'studiou-wc-ord-print-statuses');
  12. $bulk_actions['set_status_to_in_printing'] = __('Set Status to In Printing', 'studiou-wc-ord-print-statuses');
  13. UtilsLog::log('Bulk actions registered');
  14. return $bulk_actions;
  15. }
  16. public function handle_bulk_actions($redirect_to, $action, $post_ids) {
  17. switch ($action) {
  18. case 'prepare_to_printing_export':
  19. $redirect_to = $this->prepare_to_printing_export($post_ids, $redirect_to);
  20. break;
  21. case 'set_status_to_prepare_to_printing':
  22. $redirect_to = $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
  23. break;
  24. case 'set_status_to_in_printing':
  25. $redirect_to = $this->set_status_to_in_printing($post_ids, $redirect_to);
  26. break;
  27. }
  28. return $redirect_to;
  29. }
  30. private function prepare_to_printing_export($post_ids, $redirect_to) {
  31. global $wpdb;
  32. $orders_data = $wpdb->get_results("SELECT * FROM `vsu_orders_car_prod_qty_img` WHERE order_no IN (" . implode(',', array_map('intval', $post_ids)) . ")");
  33. $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
  34. UtilsLog::log('Order statuses set to "to-print" after export.');
  35. if (!empty($orders_data)) {
  36. $csv_data = $this->generate_csv($orders_data);
  37. UtilsLog::log('file prepare_to_printing_export.csv exported');
  38. $this->output_csv($csv_data, 'prepare_to_printing_export.csv');
  39. }
  40. return $redirect_to;
  41. }
  42. private function set_status_to_prepare_to_printing($post_ids, $redirect_to) {
  43. foreach ($post_ids as $post_id) {
  44. UtilsLog::log('Setting status Order #' . $post_id . ' to "to-print"');
  45. $order = wc_get_order($post_id);
  46. if ($order) {
  47. $order->update_status('to-print');
  48. update_post_meta($post_id, 'to-print-date', current_time('mysql'));
  49. UtilsLog::log('Order #' . $post_id . ' marked as "to-print"');
  50. }
  51. else{
  52. UtilsLog::log('Order #' . $post_id . ' not found');
  53. }
  54. }
  55. $redirect_to = add_query_arg('bulk_action', 'marked_prepare_to_printing', $redirect_to);
  56. return $redirect_to;
  57. }
  58. private function set_status_to_in_printing($post_ids, $redirect_to) {
  59. foreach ($post_ids as $post_id) {
  60. $order = wc_get_order($post_id);
  61. if ($order) {
  62. $order->update_status('in-print');
  63. update_post_meta($post_id, 'in-print-date', current_time('mysql'));
  64. }
  65. }
  66. $redirect_to = add_query_arg('bulk_action', 'marked_in_printing', $redirect_to);
  67. return $redirect_to;
  68. }
  69. private function generate_csv($data) {
  70. $output = fopen('php://temp', 'w');
  71. fputcsv($output, array_keys((array)$data[0]),';'); // Headers
  72. foreach ($data as $row) {
  73. fputcsv($output, (array)$row, ';');
  74. }
  75. rewind($output);
  76. $csv = stream_get_contents($output);
  77. fclose($output);
  78. return $csv;
  79. }
  80. private function output_csv($csv_data, $filename) {
  81. header('Content-Type: text/csv');
  82. header('Content-Disposition: attachment; filename="' . $filename . '"');
  83. echo $csv_data;
  84. exit;
  85. }
  86. }