/** * Query args. * * @param array $args * @param WP_REST_Request $request * @return array */ public function query_args($args, $request) { global $wpdb; // Set post_status. if ('any' !== $request['status']) { $args['post_status'] = 'wc-' . $request['status']; } else { $args['post_status'] = 'any'; } if (!empty($request['customer'])) { if (!empty($args['meta_query'])) { $args['meta_query'] = array(); } $args['meta_query'][] = array('key' => '_customer_user', 'value' => $request['customer'], 'type' => 'NUMERIC'); } // Search by product. if (!empty($request['product'])) { $order_ids = $wpdb->get_col($wpdb->prepare("\n\t\t\t\tSELECT order_id\n\t\t\t\tFROM {$wpdb->prefix}woocommerce_order_items\n\t\t\t\tWHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )\n\t\t\t\tAND order_item_type = 'line_item'\n\t\t\t ", $request['product'])); // Force WP_Query return empty if don't found any order. $order_ids = !empty($order_ids) ? $order_ids : array(0); $args['post__in'] = $order_ids; } // Search. if (!empty($args['s'])) { $order_ids = wc_order_search($args['s']); if (!empty($order_ids)) { unset($args['s']); $args['post__in'] = array_merge($order_ids, array(0)); } } return $args; }
/** * Search custom fields as well as content. * @param WP_Query $wp */ public function shop_order_search_custom_fields($wp) { global $pagenow; if ('edit.php' != $pagenow || empty($wp->query_vars['s']) || $wp->query_vars['post_type'] != 'shop_order') { return; } $post_ids = wc_order_search($_GET['s']); if (!empty($post_ids)) { // Remove "s" - we don't want to search order name. unset($wp->query_vars['s']); // so we know we're doing this. $wp->query_vars['shop_order_search'] = true; // Search by found posts. $wp->query_vars['post__in'] = array_merge($post_ids, array(0)); } }