class-order-status-manager.php 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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. add_action('woocommerce_payment_complete', array($this, 'set_order_processing_status'));
  20. }
  21. public function register_custom_order_statuses() {
  22. foreach ($this->custom_statuses as $status_slug => $status_args) {
  23. register_post_status($status_slug, array(
  24. 'label' => $status_args['label'],
  25. 'public' => true,
  26. 'exclude_from_search' => false,
  27. 'show_in_admin_all_list' => true,
  28. 'show_in_admin_status_list' => true,
  29. 'label_count' => $status_args['label_count']
  30. ));
  31. }
  32. UtilsLog::log('Statuses registered');
  33. }
  34. public function add_custom_order_statuses_to_list($order_statuses) {
  35. $new_order_statuses = array();
  36. foreach ($order_statuses as $key => $status) {
  37. $new_order_statuses[$key] = $status;
  38. if ($key === 'wc-processing') {
  39. foreach ($this->custom_statuses as $status_slug => $status_args) {
  40. $new_order_statuses[$status_slug] = $status_args['label'];
  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. UtilsLog::log('Order #' . $order_id . ' status change ' . $old_status . ' -> ' . $new_status);
  49. if ($new_status === 'to-print') {
  50. update_post_meta($order_id, 'to_print_date', current_time('mysql'));
  51. } elseif ($new_status === 'in-print') {
  52. update_post_meta($order_id, 'in_print_date', current_time('mysql'));
  53. }
  54. }
  55. public function set_order_processing_status($order_id) {
  56. UtilsLog::log('Order #' . $order_id . ' payment complete');
  57. $order = wc_get_order($order_id);
  58. if ($order && !$order->has_status('completed')) {
  59. UtilsLog::log('Order #' . $order_id . ' will be move to processing');
  60. $order->update_status('processing', __('Payment received, order is now processing.', 'studiou-wc-ord-print-statuses'));
  61. // Add a note to the order
  62. $order->add_order_note(__('Order automatically set to processing by Studiou WC Order Print Statuses plugin.', 'studiou-wc-ord-print-statuses'));
  63. }
  64. }
  65. public function get_custom_statuses() {
  66. return $this->custom_statuses;
  67. }
  68. }