class-order-status-manager.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. <?php
  2. // Handles custom order status registration and integration.
  3. defined('ABSPATH') || exit;
  4. class Order_Status_Manager {
  5. private $custom_statuses;
  6. private $print_status_slugs = array('to-print', 'in-print');
  7. public function __construct() {
  8. add_action('init', array($this, 'register_custom_order_statuses'));
  9. add_filter('wc_order_statuses', array($this, 'add_custom_order_statuses_to_list'));
  10. add_action('woocommerce_order_status_changed', array($this, 'handle_status_transitions'), 10, 3);
  11. add_action('woocommerce_payment_complete', array($this, 'set_order_processing_status'));
  12. }
  13. private function get_custom_status_definitions() {
  14. if ($this->custom_statuses === null) {
  15. $this->custom_statuses = array(
  16. 'wc-to-print' => array(
  17. 'label' => _x('Prepare to Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
  18. 'label_count' => _n_noop(
  19. 'Prepare to Printing <span class="count">(%s)</span>',
  20. 'Prepare to Printing <span class="count">(%s)</span>',
  21. 'studiou-wc-ord-print-statuses'
  22. ),
  23. ),
  24. 'wc-in-print' => array(
  25. 'label' => _x('In Printing', 'Order status', 'studiou-wc-ord-print-statuses'),
  26. 'label_count' => _n_noop(
  27. 'In Printing <span class="count">(%s)</span>',
  28. 'In Printing <span class="count">(%s)</span>',
  29. 'studiou-wc-ord-print-statuses'
  30. ),
  31. ),
  32. );
  33. }
  34. return $this->custom_statuses;
  35. }
  36. public function register_custom_order_statuses() {
  37. foreach ($this->get_custom_status_definitions() as $status_slug => $status_args) {
  38. register_post_status($status_slug, array(
  39. 'label' => $status_args['label'],
  40. 'public' => true,
  41. 'exclude_from_search' => false,
  42. 'show_in_admin_all_list' => true,
  43. 'show_in_admin_status_list' => true,
  44. 'label_count' => $status_args['label_count'],
  45. ));
  46. }
  47. }
  48. public function add_custom_order_statuses_to_list($order_statuses) {
  49. $new_order_statuses = array();
  50. foreach ($order_statuses as $key => $status) {
  51. $new_order_statuses[$key] = $status;
  52. if ($key === 'wc-processing') {
  53. foreach ($this->get_custom_status_definitions() as $status_slug => $status_args) {
  54. $new_order_statuses[$status_slug] = $status_args['label'];
  55. }
  56. }
  57. }
  58. return $new_order_statuses;
  59. }
  60. /**
  61. * Stamp the relevant meta key when an order transitions into a print status.
  62. * Uses the order object (HPOS-safe) rather than update_post_meta.
  63. *
  64. * Note: bulk and import paths in Studiou_DB_Manager also stamp the same
  65. * meta key explicitly. That redundancy is intentional — the bulk path
  66. * always re-stamps (a re-run should refresh the timestamp), while this
  67. * listener handles manual single-order transitions originating from the
  68. * order edit screen. The "only if empty" guard below preserves the
  69. * original timestamp for manual flows; the bulk path overwrites it via
  70. * its own explicit call.
  71. */
  72. public function handle_status_transitions($order_id, $old_status, $new_status) {
  73. UtilsLog::log('Order #' . $order_id . ' status change ' . $old_status . ' -> ' . $new_status);
  74. if (!in_array($new_status, $this->print_status_slugs, true)) {
  75. return;
  76. }
  77. $order = wc_get_order($order_id);
  78. if (!$order) {
  79. return;
  80. }
  81. $meta_key = ($new_status === 'to-print') ? 'to_print_date' : 'in_print_date';
  82. if (!$order->get_meta($meta_key)) {
  83. $order->update_meta_data($meta_key, current_time('mysql'));
  84. $order->save();
  85. }
  86. }
  87. /**
  88. * Move paid orders to "processing" on payment completion — except when the
  89. * order is already past that point (completed) or has been advanced into the
  90. * print pipeline (to-print, in-print). Otherwise late payments would silently
  91. * roll a print-state order back to processing.
  92. */
  93. public function set_order_processing_status($order_id) {
  94. $order = wc_get_order($order_id);
  95. if (!$order) {
  96. return;
  97. }
  98. $protected = array_merge(array('completed'), $this->print_status_slugs);
  99. if ($order->has_status($protected)) {
  100. return;
  101. }
  102. // Pass the explanatory note to update_status — that's the canonical
  103. // place for a status-change note. Adding a second note via
  104. // add_order_note() would double-log the same event in the order
  105. // activity feed.
  106. $order->update_status(
  107. 'processing',
  108. __('Payment received — order automatically set to processing by Studiou WC Order Print Statuses plugin.', 'studiou-wc-ord-print-statuses')
  109. );
  110. }
  111. public function get_custom_statuses() {
  112. return $this->get_custom_status_definitions();
  113. }
  114. }