studiou-wc-ord-print-statuses.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. <?php
  2. /**
  3. * Plugin Name: QDR - Studiou WC Order Print Statuses
  4. * Plugin URI: https://www.quadarax.com/plugins/studiou-wc-ord-print-statuses
  5. * Description: Adds custom order statuses (to-print,in-print), bulk actions, and status change timestamps to WooCommerce for print orders to third party provider
  6. * Version: 1.0
  7. * Author: Dalibor Votruba
  8. * Author URI: https://www.quadarax.com
  9. * License: GPL2
  10. */
  11. // Exit if accessed directly
  12. if (!defined('ABSPATH')) {
  13. exit;
  14. }
  15. class Studiou_WC_Order_Print_Statuses {
  16. /**
  17. * Constructor: Sets up the necessary action and filter hooks
  18. */
  19. public function __construct() {
  20. // Hook for registering custom order status
  21. add_action('init', array($this, 'register_custom_order_status'));
  22. // Filter to add custom status to WooCommerce order statuses list
  23. add_filter('wc_order_statuses', array($this, 'add_to_print_order_status_to_list'));
  24. // Filter to add custom status to WooCommerce order statuses list
  25. add_filter('wc_order_statuses', array($this, 'add_in_print_order_status_to_list'));
  26. // Filter to add custom status to bulk actions dropdown
  27. add_filter('bulk_actions-edit-shop_order', array($this, 'add_to_print_order_status_to_bulk_actions'));
  28. // Filter to add custom status to bulk actions dropdown
  29. add_filter('bulk_actions-edit-shop_order', array($this, 'add_in_print_order_status_to_bulk_actions'));
  30. // Filter to handle the custom bulk action
  31. add_filter('handle_bulk_actions-edit-shop_order', array($this, 'handle_custom_order_status_bulk_action'), 10, 3);
  32. // Action to add timestamp when order status changes
  33. add_action('woocommerce_order_status_changed', array($this, 'add_status_change_timestamp'), 10, 4);
  34. // Filter to add custom column to orders table
  35. add_filter('manage_edit-shop_order_columns', array($this, 'add_order_status_changed_column'));
  36. // Action to populate the custom column
  37. add_action('manage_shop_order_posts_custom_column', array($this, 'populate_order_status_changed_column'), 10, 2);
  38. }
  39. /**
  40. * Registers the custom order status with WordPress
  41. */
  42. public function register_custom_order_status() {
  43. register_post_status('wc-to-print', array(
  44. 'label' => 'Prepare to Printing',
  45. 'public' => true,
  46. 'exclude_from_search' => false,
  47. 'show_in_admin_all_list' => true,
  48. 'show_in_admin_status_list' => true,
  49. 'label_count' => _n_noop('Prepare to Printing <span class="count">(%s)</span>', 'Prepare to Printing <span class="count">(%s)</span>')
  50. ));
  51. register_post_status('wc-in-print', array(
  52. 'label' => 'In Printing',
  53. 'public' => true,
  54. 'exclude_from_search' => false,
  55. 'show_in_admin_all_list' => true,
  56. 'show_in_admin_status_list' => true,
  57. 'label_count' => _n_noop('In Printing <span class="count">(%s)</span>', 'In Printing <span class="count">(%s)</span>')
  58. ));
  59. }
  60. /**
  61. * Adds the wc-to-print order status to the list of WooCommerce order statuses
  62. *
  63. * @param array $order_statuses Existing order statuses
  64. * @return array Modified order statuses
  65. */
  66. public function add_to_print_order_status_to_list($order_statuses) {
  67. $new_order_statuses = array();
  68. // Add the custom status after the "Processing" status
  69. foreach ($order_statuses as $key => $status) {
  70. $new_order_statuses[$key] = $status;
  71. if ($key === 'wc-processing') {
  72. $new_order_statuses['wc-to-print'] = 'Prepare to Printing';
  73. }
  74. }
  75. return $new_order_statuses;
  76. }
  77. /**
  78. * Adds the wc-in-print order status to the list of WooCommerce order statuses
  79. *
  80. * @param array $order_statuses Existing order statuses
  81. * @return array Modified order statuses
  82. */
  83. public function add_in_print_order_status_to_list($order_statuses) {
  84. $new_order_statuses = array();
  85. // Add the custom status after the "Processing" status
  86. foreach ($order_statuses as $key => $status) {
  87. $new_order_statuses[$key] = $status;
  88. if ($key === 'wc-to-print') {
  89. $new_order_statuses['wc-in-print'] = 'In Printing';
  90. }
  91. }
  92. return $new_order_statuses;
  93. }
  94. /**
  95. * Adds the wc-to-print order status to the bulk actions dropdown
  96. *
  97. * @param array $bulk_actions Existing bulk actions
  98. * @return array Modified bulk actions
  99. */
  100. public function add_to_print_order_status_to_bulk_actions($bulk_actions) {
  101. $bulk_actions['mark_to_print'] = 'Change status to Prepare to Printing';
  102. return $bulk_actions;
  103. }
  104. /**
  105. * Adds the wc-in-print order status to the bulk actions dropdown
  106. *
  107. * @param array $bulk_actions Existing bulk actions
  108. * @return array Modified bulk actions
  109. */
  110. public function add_in_print_order_status_to_bulk_actions($bulk_actions) {
  111. $bulk_actions['mark_in_print'] = 'Change status to In Printing';
  112. return $bulk_actions;
  113. }
  114. ends here
  115. /**
  116. * Handles the custom order status bulk action
  117. *
  118. * @param string $redirect_to The redirect URL
  119. * @param string $action The bulk action being taken
  120. * @param array $post_ids The IDs of the affected orders
  121. * @return string The modified redirect URL
  122. */
  123. public function handle_custom_order_status_bulk_action($redirect_to, $action, $post_ids) {
  124. if ($action !== 'mark_custom-status') {
  125. return $redirect_to;
  126. }
  127. foreach ($post_ids as $post_id) {
  128. $order = wc_get_order($post_id);
  129. $order->update_status('custom-status', 'Order status changed by bulk edit:', true);
  130. }
  131. $redirect_to = add_query_arg('bulk_custom_status_orders_count', count($post_ids), $redirect_to);
  132. return $redirect_to;
  133. }
  134. /**
  135. * Adds a timestamp meta data when an order status is changed
  136. *
  137. * @param int $order_id The ID of the order being changed
  138. * @param string $old_status The old status of the order
  139. * @param string $new_status The new status of the order
  140. * @param WC_Order $order The order object
  141. */
  142. public function add_status_change_timestamp($order_id, $old_status, $new_status, $order) {
  143. $timestamp = current_time('mysql');
  144. update_post_meta($order_id, '_status_changed_timestamp', $timestamp);
  145. }
  146. /**
  147. * Adds a new column to the orders table for the status change timestamp
  148. *
  149. * @param array $columns Existing columns in the orders table
  150. * @return array Modified columns
  151. */
  152. public function add_order_status_changed_column($columns) {
  153. $new_columns = array();
  154. foreach ($columns as $column_name => $column_info) {
  155. $new_columns[$column_name] = $column_info;
  156. if ($column_name === 'order_status') {
  157. $new_columns['order_status_changed'] = __('Status Changed', 'woocommerce');
  158. }
  159. }
  160. return $new_columns;
  161. }
  162. /**
  163. * Populates the custom column with the status change timestamp
  164. *
  165. * @param string $column The name of the column to populate
  166. * @param int $post_id The ID of the order
  167. */
  168. public function populate_order_status_changed_column($column, $post_id) {
  169. if ($column == 'order_status_changed') {
  170. $timestamp = get_post_meta($post_id, '_status_changed_timestamp', true);
  171. if ($timestamp) {
  172. echo date_i18n(get_option('date_format') . ' ' . get_option('time_format'), strtotime($timestamp));
  173. } else {
  174. echo '—';
  175. }
  176. }
  177. }
  178. }
  179. // Instantiate the plugin class
  180. new Studiou_WC_Order_Print_Statuses();