class-order-status-manager.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. public function handle_status_transitions($order_id, $old_status, $new_status) {
  65. UtilsLog::log('Order #' . $order_id . ' status change ' . $old_status . ' -> ' . $new_status);
  66. if (!in_array($new_status, $this->print_status_slugs, true)) {
  67. return;
  68. }
  69. $order = wc_get_order($order_id);
  70. if (!$order) {
  71. return;
  72. }
  73. $meta_key = ($new_status === 'to-print') ? 'to_print_date' : 'in_print_date';
  74. if (!$order->get_meta($meta_key)) {
  75. $order->update_meta_data($meta_key, current_time('mysql'));
  76. $order->save();
  77. }
  78. }
  79. /**
  80. * Move paid orders to "processing" on payment completion — except when the
  81. * order is already past that point (completed) or has been advanced into the
  82. * print pipeline (to-print, in-print). Otherwise late payments would silently
  83. * roll a print-state order back to processing.
  84. */
  85. public function set_order_processing_status($order_id) {
  86. $order = wc_get_order($order_id);
  87. if (!$order) {
  88. return;
  89. }
  90. $protected = array_merge(array('completed'), $this->print_status_slugs);
  91. if ($order->has_status($protected)) {
  92. return;
  93. }
  94. $order->update_status(
  95. 'processing',
  96. __('Payment received, order is now processing.', 'studiou-wc-ord-print-statuses')
  97. );
  98. $order->add_order_note(
  99. __('Order automatically set to processing by Studiou WC Order Print Statuses plugin.', 'studiou-wc-ord-print-statuses')
  100. );
  101. }
  102. public function get_custom_statuses() {
  103. return $this->get_custom_status_definitions();
  104. }
  105. }