class-custom-columns-manager.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. // Manages custom columns in the WooCommerce orders list (HPOS and legacy).
  3. defined('ABSPATH') || exit;
  4. class Custom_Columns_Manager {
  5. public function __construct() {
  6. add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'add_custom_shop_order_column'));
  7. add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'add_custom_shop_order_column_content'), 10, 2);
  8. }
  9. public function add_custom_shop_order_column($columns) {
  10. $new_columns = array();
  11. foreach ($columns as $column_name => $column_info) {
  12. $new_columns[$column_name] = $column_info;
  13. if ($column_name === 'order_total') {
  14. $new_columns['to_print_date'] = __('To Print Date', 'studiou-wc-ord-print-statuses');
  15. $new_columns['in_print_date'] = __('In Print Date', 'studiou-wc-ord-print-statuses');
  16. $new_columns['external_order_number'] = __('External Order Number', 'studiou-wc-ord-print-statuses');
  17. }
  18. }
  19. return $new_columns;
  20. }
  21. /**
  22. * Render cell content. Under HPOS WC passes the WC_Order object as the
  23. * second argument; under legacy CPT it passes the post ID. wc_get_order()
  24. * normalises both forms, so we accept either.
  25. */
  26. public function add_custom_shop_order_column_content($column, $order_or_id) {
  27. $order = wc_get_order($order_or_id);
  28. if (!$order) {
  29. echo '—';
  30. return;
  31. }
  32. switch ($column) {
  33. case 'to_print_date':
  34. echo $this->format_meta_date($order->get_meta('to_print_date'));
  35. break;
  36. case 'in_print_date':
  37. echo $this->format_meta_date($order->get_meta('in_print_date'));
  38. break;
  39. case 'external_order_number':
  40. $value = $order->get_meta('external_ref_ord_no');
  41. echo $value !== '' ? esc_html($value) : '—';
  42. break;
  43. }
  44. }
  45. private function format_meta_date($value) {
  46. if (empty($value)) {
  47. return '—';
  48. }
  49. $timestamp = strtotime($value);
  50. if (!$timestamp) {
  51. return '—';
  52. }
  53. return esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $timestamp));
  54. }
  55. }