class-bulk-actions-manager.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. return $bulk_actions;
  14. }
  15. public function handle_bulk_actions($redirect_to, $action, $post_ids) {
  16. switch ($action) {
  17. case 'prepare_to_printing_export':
  18. $redirect_to = $this->prepare_to_printing_export($post_ids, $redirect_to);
  19. break;
  20. case 'set_status_to_prepare_to_printing':
  21. $redirect_to = $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
  22. break;
  23. case 'set_status_to_in_printing':
  24. $redirect_to = $this->set_status_to_in_printing($post_ids, $redirect_to);
  25. break;
  26. }
  27. return $redirect_to;
  28. }
  29. private function prepare_to_printing_export($post_ids, $redirect_to) {
  30. global $wpdb;
  31. $orders_data = $wpdb->get_results("SELECT * FROM `vsu_orders_car_prod_qty_img` WHERE post_id IN (" . implode(',', array_map('intval', $post_ids)) . ")");
  32. if (!empty($orders_data)) {
  33. $csv_data = $this->generate_csv($orders_data);
  34. $this->output_csv($csv_data, 'prepare_to_printing_export.csv');
  35. }
  36. $this->set_status_to_prepare_to_printing($post_ids, $redirect_to);
  37. return $redirect_to;
  38. }
  39. private function set_status_to_prepare_to_printing($post_ids, $redirect_to) {
  40. foreach ($post_ids as $post_id) {
  41. $order = wc_get_order($post_id);
  42. if ($order) {
  43. $order->update_status('to-print');
  44. update_post_meta($post_id, 'to-print-date', current_time('mysql'));
  45. }
  46. }
  47. $redirect_to = add_query_arg('bulk_action', 'marked_prepare_to_printing', $redirect_to);
  48. return $redirect_to;
  49. }
  50. private function set_status_to_in_printing($post_ids, $redirect_to) {
  51. foreach ($post_ids as $post_id) {
  52. $order = wc_get_order($post_id);
  53. if ($order) {
  54. $order->update_status('in-print');
  55. update_post_meta($post_id, 'in-print-date', current_time('mysql'));
  56. }
  57. }
  58. $redirect_to = add_query_arg('bulk_action', 'marked_in_printing', $redirect_to);
  59. return $redirect_to;
  60. }
  61. private function generate_csv($data) {
  62. $output = fopen('php://temp', 'w');
  63. fputcsv($output, array_keys((array)$data[0])); // Headers
  64. foreach ($data as $row) {
  65. fputcsv($output, (array)$row);
  66. }
  67. rewind($output);
  68. $csv = stream_get_contents($output);
  69. fclose($output);
  70. return $csv;
  71. }
  72. private function output_csv($csv_data, $filename) {
  73. header('Content-Type: text/csv');
  74. header('Content-Disposition: attachment; filename="' . $filename . '"');
  75. echo $csv_data;
  76. exit;
  77. }
  78. }