WC_REST_Product_Variations_Controller::prepare_objects_query() │ protected │ WC 3.0.0
Prepare objects query.
Method of the class: WC_REST_Product_Variations_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)
- Full details about the request.
Changelog
Since 3.0.0 | Introduced. |
WC_REST_Product_Variations_Controller::prepare_objects_query() WC REST Product Variations Controller::prepare objects query code WC 9.8.1
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 variation 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 product variations. 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'] ), ) ); } /** * @deprecated 8.1.0 replaced by attributes. * Filter by local attributes. */ if ( ! empty( $request['local_attributes'] ) && is_array( $request['local_attributes'] ) ) { wc_deprecated_argument( 'local_attributes', '8.1', 'Use "attributes" instead.' ); foreach ( $request['local_attributes'] as $attribute ) { if ( ! isset( $attribute['attribute'] ) || ! isset( $attribute['term'] ) ) { continue; } $args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args, array( 'key' => 'attribute_' . $attribute['attribute'], 'value' => $attribute['term'], ) ); } } // Filter by attributes. if ( ! empty( $request['attributes'] ) && is_array( $request['attributes'] ) ) { foreach ( $request['attributes'] as $attribute ) { if ( isset( $attribute['attribute'] ) ) { if ( isset( $attribute['term'] ) ) { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args, array( 'key' => 'attribute_' . $attribute['attribute'], 'value' => $attribute['term'], ) ); } elseif ( ! empty( $attribute['terms'] ) && is_array( $attribute['terms'] ) ) { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query $args, array( 'key' => 'attribute_' . $attribute['attribute'], 'compare' => 'IN', 'value' => $attribute['terms'], ), ); } } } } // 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', ) ); } // Filter by global_unique_id. 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. } // Price filter. if ( is_bool( $request['has_price'] ) ) { if ( $request['has_price'] ) { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore Standard.Category.SniffName.ErrorCode slow query ok. $args, array( 'relation' => 'AND', array( 'key' => '_price', 'compare' => 'EXISTS', ), array( 'key' => '_price', 'compare' => '!=', 'value' => null, ), ) ); } else { $args['meta_query'] = $this->add_meta_query( // phpcs:ignore Standard.Category.SniffName.ErrorCode slow query ok. $args, array( 'relation' => 'OR', array( 'key' => '_price', 'compare' => 'NOT EXISTS', ), array( 'key' => '_price', 'compare' => '=', 'value' => null, ), ) ); } } // Filter product based on 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['global_unique_id'] ) ) { $args['post_type'] = array( 'product', 'product_variation' ); } else { $args['post_type'] = $this->post_type; } // Filter virtual product variations. 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'] ), ) ); } $args['post_parent'] = $request['product_id']; return $args; }