class-order-status-manager.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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. }
  32. public function add_custom_order_statuses_to_list($order_statuses) {
  33. $new_order_statuses = array();
  34. foreach ($order_statuses as $key => $status) {
  35. $new_order_statuses[$key] = $status;
  36. if ($key === 'wc-processing') {
  37. foreach ($this->custom_statuses as $status_slug => $status_args) {
  38. $new_order_statuses[$status_slug] = $status_args['label'];
  39. }
  40. }
  41. }
  42. return $new_order_statuses;
  43. }
  44. public function handle_status_transitions($order_id, $old_status, $new_status) {
  45. // Handle status transitions here
  46. // For example:
  47. if ($new_status === 'to-print') {
  48. update_post_meta($order_id, 'to-print-date', current_time('mysql'));
  49. } elseif ($new_status === 'in-print') {
  50. update_post_meta($order_id, 'in-print-date', current_time('mysql'));
  51. }
  52. }
  53. public function get_custom_statuses() {
  54. return $this->custom_statuses;
  55. }
  56. }