WC_REST_Orders_V2_Controller::prepare_objects_query
Prepare objects query.
Method of the class: WC_REST_Orders_V2_Controller{}
Hooks from the method
Returns
Array.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_objects_query( $request );
- $request(WP_REST_Request) (required)
- Full details about the request.
Changelog
| Since 3.0.0 | Introduced. |
WC_REST_Orders_V2_Controller::prepare_objects_query() WC REST Orders V2 Controller::prepare objects query code WC 10.4.3
protected function prepare_objects_query( $request ) {
global $wpdb;
$args = parent::prepare_objects_query( $request );
// Set post_status.
if ( in_array( $request['status'], $this->get_order_statuses(), true ) ) {
$args['post_status'] = 'wc-' . $request['status'];
} elseif ( 'any' === $request['status'] ) {
$args['post_status'] = 'any';
} else {
$args['post_status'] = $request['status'];
}
if ( isset( $request['customer'] ) ) {
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$args['customer_id'] = $request['customer'];
} else {
if ( ! empty( $args['meta_query'] ) ) {
$args['meta_query'] = array(); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}
$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(
"SELECT order_id
FROM {$wpdb->prefix}woocommerce_order_items
WHERE order_item_id IN ( SELECT order_item_id FROM {$wpdb->prefix}woocommerce_order_itemmeta WHERE meta_key = '_product_id' AND meta_value = %d )
AND order_item_type = 'line_item'",
$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 ( ! OrderUtil::custom_orders_table_usage_is_enabled() && ! 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 ) );
}
}
/**
* Filter the query arguments for a request.
*
* Enables adding extra arguments or setting defaults for an order collection request.
*
* @param array $args Key value array of query var to query value.
* @param WP_REST_Request $request The request used.
*
* @since 4.5.0.
*/
$args = apply_filters( 'woocommerce_rest_orders_prepare_object_query', $args, $request );
return $args;
}