hpos_enabled()) { $screen_id = wc_get_page_screen_id('shop_order'); add_filter("manage_{$screen_id}_columns", array($this, 'add_order_columns')); add_action("manage_{$screen_id}_custom_column", array($this, 'populate_order_columns'), 10, 2); add_action('woocommerce_order_list_table_restrict_manage_orders', array($this, 'add_order_filters')); add_filter('woocommerce_order_list_table_prepare_items_query_args', array($this, 'filter_orders_by_company_hpos')); } else { add_filter('manage_edit-shop_order_columns', array($this, 'add_order_columns')); add_action('manage_shop_order_posts_custom_column', array($this, 'populate_order_columns'), 10, 2); add_action('restrict_manage_posts', array($this, 'add_order_filters')); add_filter('parse_query', array($this, 'filter_orders_by_company')); } // Add order meta boxes add_action('add_meta_boxes', array($this, 'add_order_meta_boxes')); } /** * Whether HPOS is the active order storage. */ private function hpos_enabled() { return class_exists('\Automattic\WooCommerce\Utilities\OrderUtil') && \Automattic\WooCommerce\Utilities\OrderUtil::custom_orders_table_usage_is_enabled(); } /** * The company filter on the WooCommerce order list (a read-only view * filter, so no nonce). */ private function query_company() { return isset($_GET['crewcommerce_company']) ? absint(wp_unslash($_GET['crewcommerce_company'])) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended } private function query_approval_status() { $value = isset($_GET['crewcommerce_approval']) ? sanitize_key(wp_unslash($_GET['crewcommerce_approval'])) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended return in_array($value, array('pending', 'approved', 'rejected', 'no_approval'), true) ? $value : ''; } /** * Add custom columns to orders list */ public function add_order_columns($columns) { $new_columns = array(); foreach ($columns as $key => $column) { $new_columns[$key] = $column; if ($key === 'order_status') { $new_columns['company'] = __('Company', 'crewcommerce'); $new_columns['requester'] = __('Requester', 'crewcommerce'); $new_columns['approval_status'] = __('Approval Status', 'crewcommerce'); } } return $new_columns; } /** * Populate custom order columns */ public function populate_order_columns($column, $post_id) { $order = wc_get_order($post_id); if (!$order) { return; } switch ($column) { case 'company': $customer_id = $order->get_customer_id(); $company = null; if ($customer_id) { $team_accounts = CrewCommerce_Team_Accounts::get_instance(); $company_id = $team_accounts->get_user_company($customer_id); $company = $company_id ? get_post($company_id) : null; } echo $company ? esc_html($company->post_title) : '—'; break; case 'requester': $customer_id = $order->get_customer_id(); if ($customer_id) { $user = get_user_by('ID', $customer_id); if ($user) { echo esc_html($user->display_name); } else { echo '—'; } } else { echo esc_html($order->get_billing_first_name() . ' ' . $order->get_billing_last_name()); } break; case 'approval_status': $approvals = CrewCommerce_Approvals::get_instance(); // Under HPOS this is a WC_Order, not a post ID. $workflow = $approvals->get_order_workflow($order->get_id()); if ($workflow) { $labels = CrewCommerce_Admin_Dashboard::workflow_status_labels(); printf( '%2$s', esc_attr($workflow->status), esc_html(isset($labels[$workflow->status]) ? $labels[$workflow->status] : ucfirst($workflow->status)) ); } else { echo '—'; } break; } } /** * Company and approval filters on the Orders list. Hooked for both the * classic and the HPOS list, which pass the same two arguments. */ public function add_order_filters($post_type = 'shop_order', $which = '') { if ($post_type !== 'shop_order') { return; } // Company filter $companies = get_posts(array( 'post_type' => 'crewcommerce_company', 'posts_per_page' => -1, 'post_status' => 'publish' )); if (!empty($companies)) { $selected_company = $this->query_company(); echo ''; } // Approval status filter $selected_approval = $this->query_approval_status(); $options = array( '' => __('All Approval Statuses', 'crewcommerce'), 'pending' => __('Pending Approval', 'crewcommerce'), 'approved' => __('Approved', 'crewcommerce'), 'rejected' => __('Rejected', 'crewcommerce'), 'no_approval' => __('No Approval Required', 'crewcommerce'), ); echo ''; } /** * Filter orders by company and approval status */ public function filter_orders_by_company($query) { global $pagenow, $typenow; if ($pagenow !== 'edit.php' || $typenow !== 'shop_order') { return; } $company_id = $this->query_company(); $approval_status = $this->query_approval_status(); if ($company_id || $approval_status) { $meta_query = array(); if ($company_id) { // Get users from this company $team_accounts = CrewCommerce_Team_Accounts::get_instance(); $team_members = $team_accounts->get_team_members($company_id); $user_ids = wp_list_pluck($team_members, 'user_id'); if (!empty($user_ids)) { $meta_query[] = array( 'key' => '_customer_user', 'value' => $user_ids, 'compare' => 'IN' ); } } if ($approval_status && $approval_status !== 'no_approval') { global $wpdb; // Get order IDs with specific approval status $order_ids = $wpdb->get_col($wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}crewcommerce_approval_workflows WHERE status = %s", $approval_status )); if (!empty($order_ids)) { $query->set('post__in', $order_ids); } else { // No orders found with this status $query->set('post__in', array(0)); } } elseif ($approval_status === 'no_approval') { global $wpdb; // Get order IDs without approval workflows $order_ids_with_approval = $wpdb->get_col( "SELECT DISTINCT order_id FROM {$wpdb->prefix}crewcommerce_approval_workflows" ); if (!empty($order_ids_with_approval)) { $query->set('post__not_in', $order_ids_with_approval); } } if (!empty($meta_query)) { $query->set('meta_query', $meta_query); } } } /** * HPOS version of filter_orders_by_company(), as query args. * Under HPOS 'post__in' maps to the order ID and 'customer' replaces * the _customer_user meta. */ public function filter_orders_by_company_hpos($query_args) { $company_id = $this->query_company(); $approval_status = $this->query_approval_status(); if ($company_id) { $team_accounts = CrewCommerce_Team_Accounts::get_instance(); $team_members = $team_accounts->get_team_members($company_id); $user_ids = wp_list_pluck($team_members, 'user_id'); $query_args['customer'] = !empty($user_ids) ? $user_ids : array(0); } if ($approval_status && $approval_status !== 'no_approval') { global $wpdb; $order_ids = $wpdb->get_col($wpdb->prepare( "SELECT order_id FROM {$wpdb->prefix}crewcommerce_approval_workflows WHERE status = %s", $approval_status )); $query_args['post__in'] = !empty($order_ids) ? $order_ids : array(0); } elseif ($approval_status === 'no_approval') { global $wpdb; $order_ids_with_approval = $wpdb->get_col( "SELECT DISTINCT order_id FROM {$wpdb->prefix}crewcommerce_approval_workflows" ); if (!empty($order_ids_with_approval)) { $query_args['post__not_in'] = $order_ids_with_approval; } } return $query_args; } /** * Add order meta boxes */ public function add_order_meta_boxes() { // Under HPOS the order screen isn't 'shop_order'. $screen_id = $this->hpos_enabled() ? wc_get_page_screen_id('shop_order') : 'shop_order'; add_meta_box( 'crewcommerce_order_info', __('CrewCommerce Order Info', 'crewcommerce'), array($this, 'render_order_info_meta_box'), $screen_id, 'side', 'high' ); } /** * Render order info meta box */ public function render_order_info_meta_box($post_or_order) { // A WP_Post, or a WC_Order under HPOS. wc_get_order() takes either. $order = wc_get_order($post_or_order); if (!$order) { return; } $customer_id = $order->get_customer_id(); $team_accounts = CrewCommerce_Team_Accounts::get_instance(); $approvals = CrewCommerce_Approvals::get_instance(); // Get company info $company_id = $customer_id ? $team_accounts->get_user_company($customer_id) : 0; $company = $company_id ? get_post($company_id) : null; // Get user role $user_role = $customer_id ? $team_accounts->get_user_role($customer_id) : ''; $workflow = $approvals->get_order_workflow($order->get_id()); $labels = CrewCommerce_Admin_Dashboard::workflow_status_labels(); $roles = array( 'admin' => __('Admin', 'crewcommerce'), 'manager' => __('Manager', 'crewcommerce'), 'requester' => __('Requester', 'crewcommerce'), ); ?>
post_title : __('No company', 'crewcommerce')); ?>
status]) ? $labels[$workflow->status] : ucfirst($workflow->status)); ?>
display_name); ?>
notes); ?>
created_at))); ?>