| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- <?php
- // Manages custom columns in the WooCommerce orders list (HPOS and legacy).
- defined('ABSPATH') || exit;
- class Custom_Columns_Manager {
- public function __construct() {
- add_filter('manage_woocommerce_page_wc-orders_columns', array($this, 'add_custom_shop_order_column'));
- add_action('manage_woocommerce_page_wc-orders_custom_column', array($this, 'add_custom_shop_order_column_content'), 10, 2);
- }
- public function add_custom_shop_order_column($columns) {
- $new_columns = array();
- foreach ($columns as $column_name => $column_info) {
- $new_columns[$column_name] = $column_info;
- if ($column_name === 'order_total') {
- $new_columns['to_print_date'] = __('To Print Date', 'studiou-wc-ord-print-statuses');
- $new_columns['in_print_date'] = __('In Print Date', 'studiou-wc-ord-print-statuses');
- $new_columns['external_order_number'] = __('External Order Number', 'studiou-wc-ord-print-statuses');
- }
- }
- return $new_columns;
- }
- /**
- * Render cell content. Under HPOS WC passes the WC_Order object as the
- * second argument; under legacy CPT it passes the post ID. wc_get_order()
- * normalises both forms, so we accept either.
- */
- public function add_custom_shop_order_column_content($column, $order_or_id) {
- $order = wc_get_order($order_or_id);
- if (!$order) {
- echo '—';
- return;
- }
- switch ($column) {
- case 'to_print_date':
- echo $this->format_meta_date($order->get_meta('to_print_date'));
- break;
- case 'in_print_date':
- echo $this->format_meta_date($order->get_meta('in_print_date'));
- break;
- case 'external_order_number':
- $value = $order->get_meta('external_ref_ord_no');
- echo $value !== '' ? esc_html($value) : '—';
- break;
- }
- }
- private function format_meta_date($value) {
- if (empty($value)) {
- return '—';
- }
- $timestamp = strtotime($value);
- if (!$timestamp) {
- return '—';
- }
- return esc_html(date_i18n(get_option('date_format') . ' ' . get_option('time_format'), $timestamp));
- }
- }
|