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.
Return
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 9.5.1
protected function prepare_objects_query( $request ) { $args = WC_REST_CRUD_Controller::prepare_objects_query( $request ); // Set post_status. $args['post_status'] = $request['status']; // 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. if ( ! empty( $request['type'] ) ) { $tax_query[] = array( 'taxonomy' => 'product_type', 'field' => 'slug', 'terms' => $request['type'], ); } // 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', ); } if ( wc_product_sku_enabled() ) { // Do a partial match for a sku. Supersedes sku parameter that does exact matching. if ( ! empty( $request['search_sku'] ) ) { // Store this for use in the query clause filters. $this->search_sku_in_product_lookup_table = $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'] ) ) { $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; } return $args; }