class-order-status-manager.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. // Handles custom order status registration and integration.
  3. class Order_Status_Manager {
  4. private $custom_statuses;
  5. public function __construct() {
  6. $this->custom_statuses = array(
  7. 'wc-to-print' => array(
  8. 'label' => _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
  9. 'label_count' => _n_noop('Prepare to Printing <span class="count">(%s)</span>', 'Prepare to Printing <span class="count">(%s)</span>', 'studiou-wc-ord-print-statuses')
  10. ),
  11. 'wc-in-print' => array(
  12. 'label' => _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
  13. 'label_count' => _n_noop('In Printing <span class="count">(%s)</span>', 'In Printing <span class="count">(%s)</span>', 'studiou-wc-ord-print-statuses')
  14. )
  15. );
  16. add_action('init', array($this, 'register_custom_order_statuses'));
  17. add_filter('wc_order_statuses', array($this, 'add_custom_order_statuses_to_list'));
  18. add_action('woocommerce_order_status_changed', array($this, 'handle_status_transitions'), 10, 3);
  19. }
  20. public function register_custom_order_statuses() {
  21. foreach ($this->custom_statuses as $status_slug => $status_args) {
  22. register_post_status($status_slug, array(
  23. 'label' => $status_args['label'],
  24. 'public' => true,
  25. 'exclude_from_search' => false,
  26. 'show_in_admin_all_list' => true,
  27. 'show_in_admin_status_list' => true,
  28. 'label_count' => $status_args['label_count']
  29. ));
  30. }
  31. error_log('Statuses registered');
  32. }
  33. public function add_custom_order_statuses_to_list($order_statuses) {
  34. $new_order_statuses = array();
  35. foreach ($order_statuses as $key => $status) {
  36. $new_order_statuses[$key] = $status;
  37. if ($key === 'wc-processing') {
  38. foreach ($this->custom_statuses as $status_slug => $status_args) {
  39. $new_order_statuses[$status_slug] = $status_args['label'];
  40. }
  41. }
  42. }
  43. return $new_order_statuses;
  44. }
  45. public function handle_status_transitions($order_id, $old_status, $new_status) {
  46. // Handle status transitions here
  47. // For example:
  48. if ($new_status === 'to-print') {
  49. update_post_meta($order_id, 'to-print-date', current_time('mysql'));
  50. } elseif ($new_status === 'in-print') {
  51. update_post_meta($order_id, 'in-print-date', current_time('mysql'));
  52. }
  53. }
  54. public function get_custom_statuses() {
  55. return $this->custom_statuses;
  56. }
  57. }