WC_REST_Products_Controller::prepare_objects_query │ protected │ WC 1.0
Make extra product orderby features supported by WooCommerce available to the WC API. This includes 'price', 'popularity', and 'rating'.
Method of the class: WC_REST_Products_Controller{}
No Hooks.
Returns
Array.
Usage
// protected - for code of main (parent) or child class $result = $this->prepare_objects_query( $request );
- $request(WP_REST_Request) (required)
- Request data.
WC_REST_Products_Controller::prepare_objects_query() WC REST Products Controller::prepare objects query code WC 10.3.6
protected function prepare_objects_query( $request ) {
$args = WC_REST_CRUD_Controller::prepare_objects_query( $request );
// Set post_status.
$args['post_status'] = $request['status'];
// Filter by a list of product statuses.
if ( ! empty( $request['include_status'] ) ) {
$args['post_status'] = $request['include_status'];
}
if ( ! empty( $request['exclude_status'] ) ) {
$this->exclude_status = $request['exclude_status'];
} else {
$this->exclude_status = array();
}
// Filter downloadable products.
if ( isset( $request['downloadable'] ) ) {
$args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
$args,
array(
'key' => '_downloadable',
'value' => wc_bool_to_string( $request['downloadable'] ),
)
);
}
// Filter virtual products.
if ( isset( $request['virtual'] ) ) {
$args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
$args,
array(
'key' => '_virtual',
'value' => wc_bool_to_string( $request['virtual'] ),
)
);
}
// Taxonomy query to filter products by type, category,
// tag, shipping class, and attribute.
$tax_query = array();
// Map between taxonomy name and arg's key.
$taxonomies = array(
'product_cat' => 'category',
'product_tag' => 'tag',
'product_shipping_class' => 'shipping_class',
);
// Set tax_query for each passed arg.
foreach ( $taxonomies as $taxonomy => $key ) {
if ( ! empty( $request[ $key ] ) ) {
$tax_query[] = array(
'taxonomy' => $taxonomy,
'field' => 'term_id',
'terms' => $request[ $key ],
);
}
}
// Filter product type by slug.
$terms = array();
if ( ! empty( $request['include_types'] ) ) {
$terms = $request['include_types'];
} elseif ( ! empty( $request['type'] ) ) {
$terms[] = $request['type'];
}
if ( ! empty( $terms ) ) {
$tax_query[] = array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $terms,
);
}
// Add exclude types filter.
if ( ! empty( $request['exclude_types'] ) ) {
$tax_query[] = array(
'taxonomy' => 'product_type',
'field' => 'slug',
'terms' => $request['exclude_types'],
'operator' => 'NOT IN',
);
}
// Filter by attribute and term.
if ( ! empty( $request['attribute'] ) && ! empty( $request['attribute_term'] ) ) {
if ( in_array( $request['attribute'], wc_get_attribute_taxonomy_names(), true ) ) {
$tax_query[] = array(
'taxonomy' => $request['attribute'],
'field' => 'term_id',
'terms' => $request['attribute_term'],
);
}
}
// Build tax_query if taxonomies are set.
if ( ! empty( $tax_query ) ) {
if ( ! empty( $args['tax_query'] ) ) {
$args['tax_query'] = array_merge( $tax_query, $args['tax_query'] ); // WPCS: slow query ok.
} else {
$args['tax_query'] = $tax_query; // WPCS: slow query ok.
}
}
// Filter featured.
if ( is_bool( $request['featured'] ) ) {
$args['tax_query'][] = array(
'taxonomy' => 'product_visibility',
'field' => 'name',
'terms' => 'featured',
'operator' => true === $request['featured'] ? 'IN' : 'NOT IN',
);
}
// Search parameter precedence: search_fields > search_name_or_sku > search_sku > sku.
$search_fields = $request['search_fields'] ?? array();
$search_arg = trim( $request['search'] ?? '' );
if ( $search_fields && $search_arg ) {
$tokens = array_filter( array_map( 'trim', explode( ' ', $search_arg ) ) );
$this->search_fields_tokens = array(
'fields' => $search_fields,
'tokens' => $tokens,
);
unset( $request['search'], $request['search_sku'], $request['sku'], $request['search_name_or_sku'], $args['s'] );
}
$search_name_or_sku_arg = $request['search_name_or_sku'] ?? '';
if ( '' !== $search_name_or_sku_arg ) {
// Do a tokenized search for name or SKU. Supersedes the 'search', 'search_sku' and 'sku' arguments.
$tokens = array_filter( array_map( 'trim', explode( ' ', $search_name_or_sku_arg ) ) );
$this->search_name_or_sku_tokens = $tokens;
unset( $request['search'] );
unset( $args['s'] );
unset( $request['search_sku'] );
unset( $request['sku'] );
} elseif ( wc_product_sku_enabled() ) {
// Do a partial match for a sku. Supersedes the 'sku' argument, that does exact matching.
if ( ! empty( $request['search_sku'] ) ) {
// Store this for use in the query clause filters.
$this->search_sku_arg_value = $request['search_sku'];
unset( $request['sku'] );
}
// Filter by sku.
if ( ! empty( $request['sku'] ) ) {
$skus = explode( ',', $request['sku'] );
// Include the current string as a SKU too.
if ( 1 < count( $skus ) ) {
$skus[] = $request['sku'];
}
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args,
array(
'key' => '_sku',
'value' => $skus,
'compare' => 'IN',
)
);
}
}
if ( ! empty( $request['global_unique_id'] ) ) {
$global_unique_ids = array_map( 'trim', explode( ',', $request['global_unique_id'] ) );
$args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
$args,
array(
'key' => '_global_unique_id',
'value' => $global_unique_ids,
'compare' => 'IN',
)
);
}
// Filter by tax class.
if ( ! empty( $request['tax_class'] ) ) {
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args,
array(
'key' => '_tax_class',
'value' => 'standard' !== $request['tax_class'] ? $request['tax_class'] : '',
)
);
}
// Price filter.
if ( ! empty( $request['min_price'] ) || ! empty( $request['max_price'] ) ) {
$args['meta_query'] = $this->add_meta_query( $args, wc_get_min_max_price_meta_query( $request ) ); // WPCS: slow query ok.
}
// Filter product by stock_status.
if ( ! empty( $request['stock_status'] ) ) {
$args['meta_query'] = $this->add_meta_query( // WPCS: slow query ok.
$args,
array(
'key' => '_stock_status',
'value' => $request['stock_status'],
)
);
}
// Filter by on sale products.
if ( is_bool( $request['on_sale'] ) ) {
$on_sale_key = $request['on_sale'] ? 'post__in' : 'post__not_in';
$on_sale_ids = wc_get_product_ids_on_sale();
// Use 0 when there's no on sale products to avoid return all products.
$on_sale_ids = empty( $on_sale_ids ) ? array( 0 ) : $on_sale_ids;
$args[ $on_sale_key ] += $on_sale_ids;
}
// Force the post_type argument, since it's not a user input variable.
if ( ! empty( $request['sku'] ) || ! empty( $request['search_sku'] ) || $this->search_name_or_sku_tokens || $this->search_fields_tokens ) {
$args['post_type'] = array( 'product', 'product_variation' );
} else {
$args['post_type'] = $this->post_type;
}
$ordering_args = WC()->query->get_catalog_ordering_args( $args['orderby'], $args['order'] );
$args['orderby'] = $ordering_args['orderby'];
$args['order'] = $ordering_args['order'];
if ( $ordering_args['meta_key'] ) {
$args['meta_key'] = $ordering_args['meta_key']; // WPCS: slow query ok.
}
/*
* When the suggested products ids is not empty,
* filter the query to return only the suggested products,
* overwriting the post__in parameter.
*/
if ( ! empty( $this->suggested_products_ids ) ) {
$args['post__in'] = $this->suggested_products_ids;
}
// Force the post_type argument, since it's not a user input variable.
if ( ! empty( $request['global_unique_id'] ) ) {
$args['post_type'] = array( 'product', 'product_variation' );
}
return $args;
}