class-bulk-actions-manager.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (!empty($orders_data)) {
  34. $csv_data = $this->generate_csv($orders_data);
  35. $this->output_csv($csv_data, 'prepare_to_printing_export.csv');
  36. }
  37. $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
  38. UtilsLog::log('file exported');
  39. return $redirect_to;
  40. }
  41. private function set_status_to_prepare_to_printing($post_ids, $redirect_to) {
  42. foreach ($post_ids as $post_id) {
  43. UtilsLog::log('Setting status Order #' . $post_id . ' to "to-print"');
  44. $order = wc_get_order($post_id);
  45. if ($order) {
  46. $order->update_status('to-print');
  47. update_post_meta($post_id, 'to-print-date', current_time('mysql'));
  48. UtilsLog::log('Order #' . $post_id . ' marked as "to-print"');
  49. }
  50. else{
  51. UtilsLog::log('Order #' . $post_id . ' not found');
  52. }
  53. }
  54. $redirect_to = add_query_arg('bulk_action', 'marked_prepare_to_printing', $redirect_to);
  55. return $redirect_to;
  56. }
  57. private function set_status_to_in_printing($post_ids, $redirect_to) {
  58. foreach ($post_ids as $post_id) {
  59. $order = wc_get_order($post_id);
  60. if ($order) {
  61. $order->update_status('in-print');
  62. update_post_meta($post_id, 'in-print-date', current_time('mysql'));
  63. }
  64. }
  65. $redirect_to = add_query_arg('bulk_action', 'marked_in_printing', $redirect_to);
  66. return $redirect_to;
  67. }
  68. private function generate_csv($data) {
  69. $output = fopen('php://temp', 'w');
  70. fputcsv($output, array_keys((array)$data[0]),';'); // Headers
  71. foreach ($data as $row) {
  72. fputcsv($output, (array)$row, ';');
  73. }
  74. rewind($output);
  75. $csv = stream_get_contents($output);
  76. fclose($output);
  77. return $csv;
  78. }
  79. private function output_csv($csv_data, $filename) {
  80. header('Content-Type: text/csv');
  81. header('Content-Disposition: attachment; filename="' . $filename . '"');
  82. echo $csv_data;
  83. exit;
  84. }
  85. }