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commit date
AI 1 year ago Agentic 4 months ago AbstractCartRoute.php 4 months ago AbstractRoute.php 3 months ago AbstractTermsRoute.php 4 months ago Batch.php 4 months ago Cart.php 1 year ago CartAddItem.php 5 days ago CartApplyCoupon.php 1 year ago CartCoupons.php 3 months ago CartCouponsByCode.php 1 year ago CartExtensions.php 2 years ago CartItems.php 2 years ago CartItemsByKey.php 2 years ago CartRemoveCoupon.php 1 year ago CartRemoveItem.php 4 months ago CartSelectShippingRate.php 5 months ago CartUpdateCustomer.php 4 months ago CartUpdateItem.php 5 days ago Checkout.php 5 days ago CheckoutOrder.php 5 days ago Order.php 7 months ago Patterns.php 1 year ago ProductAttributeTerms.php 1 month ago ProductAttributes.php 1 year ago ProductAttributesById.php 1 year ago ProductBrands.php 1 year ago ProductBrandsById.php 1 year ago ProductCategories.php 1 year ago ProductCategoriesById.php 1 year ago ProductCollectionData.php 5 days ago ProductReviews.php 5 days ago ProductTags.php 1 year ago Products.php 3 months ago ProductsById.php 5 days ago ProductsBySlug.php 5 days ago ShopperListItems.php 1 month ago ShopperListItemsByKey.php 1 month ago ShopperLists.php 1 month ago ShopperListsBySlug.php 1 month ago ShopperListsNonceCheck.php 1 month ago
ProductCollectionData.php
293 lines
1 <?php
2 namespace Automattic\WooCommerce\StoreApi\Routes\V1;
3
4 use Automattic\WooCommerce\StoreApi\Utilities\ProductQueryFilters;
5
6 /**
7 * ProductCollectionData route.
8 * Get aggregate data from a collection of products.
9 *
10 * Supports the same parameters as /products, but returns a different response.
11 */
12 class ProductCollectionData extends AbstractRoute {
13 /**
14 * The route identifier.
15 *
16 * @var string
17 */
18 const IDENTIFIER = 'product-collection-data';
19
20 /**
21 * The routes schema.
22 *
23 * @var string
24 */
25 const SCHEMA_TYPE = 'product-collection-data';
26
27 /**
28 * Default maximum number of entries accepted in the `calculate_attribute_counts` and
29 * `calculate_taxonomy_counts` parameters. Each entry triggers a full-collection aggregate
30 * query, so this bounds the per-request query fan-out. Matches the batch route's request cap.
31 *
32 * @var int
33 */
34 const COUNTS_MAX_ITEMS = 25;
35
36 /**
37 * Get the path of this REST route.
38 *
39 * @return string
40 */
41 public function get_path() {
42 return self::get_path_regex();
43 }
44
45 /**
46 * Get the path of this rest route.
47 *
48 * @return string
49 */
50 public static function get_path_regex() {
51 return '/products/collection-data';
52 }
53
54 /**
55 * Get method arguments for this REST route.
56 *
57 * @return array An array of endpoints.
58 */
59 public function get_args() {
60 return [
61 [
62 'methods' => \WP_REST_Server::READABLE,
63 'callback' => [ $this, 'get_response' ],
64 'permission_callback' => '__return_true',
65 'args' => $this->get_collection_params(),
66 'allow_batch' => [ 'v1' => true ],
67 ],
68 'schema' => [ $this->schema, 'get_public_item_schema' ],
69 ];
70 }
71
72 /**
73 * Get a collection of posts and add the post title filter option to \WP_Query.
74 *
75 * @param \WP_REST_Request $request Request object.
76 * @return \WP_REST_Response
77 */
78 protected function get_route_response( \WP_REST_Request $request ) {
79 $data = [
80 'min_price' => null,
81 'max_price' => null,
82 'attribute_counts' => null,
83 'stock_status_counts' => null,
84 'rating_counts' => null,
85 'taxonomy_counts' => null,
86 ];
87 $filters = new ProductQueryFilters();
88
89 if ( ! empty( $request['calculate_price_range'] ) ) {
90 $filter_request = clone $request;
91 $filter_request->set_param( 'min_price', null );
92 $filter_request->set_param( 'max_price', null );
93
94 $price_results = $filters->get_filtered_price( $filter_request );
95 $data['min_price'] = $price_results->min_price;
96 $data['max_price'] = $price_results->max_price;
97 }
98
99 if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
100 $filter_request = clone $request;
101 $counts = $filters->get_stock_status_counts( $filter_request );
102
103 $data['stock_status_counts'] = [];
104
105 foreach ( $counts as $key => $value ) {
106 $data['stock_status_counts'][] = (object) [
107 'status' => $key,
108 'count' => $value,
109 ];
110 }
111 }
112
113 if ( ! empty( $request['calculate_attribute_counts'] ) ) {
114 $taxonomy__or_queries = [];
115 $taxonomy__and_queries = [];
116
117 foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
118 if ( empty( $attributes_to_count['taxonomy'] ) ) {
119 continue;
120 }
121
122 // Normalize to the canonical taxonomy name before deduping so textual variants
123 // (e.g. differing case or surrounding whitespace) collapse to a single query.
124 $taxonomy = wc_sanitize_taxonomy_name( $attributes_to_count['taxonomy'] );
125
126 // Resolve numeric attribute IDs (e.g. "3") to their taxonomy name (e.g. "pa_color").
127 if ( is_numeric( $taxonomy ) ) {
128 $taxonomy = wc_attribute_taxonomy_name_by_id( (int) $taxonomy );
129 }
130
131 // Skip anything that is not a registered product attribute taxonomy so non-existent
132 // or non-attribute taxonomies do not trigger wasted full-collection queries.
133 if ( ! taxonomy_is_product_attribute( $taxonomy ) ) {
134 continue;
135 }
136
137 if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
138 $taxonomy__or_queries[] = $taxonomy;
139 } else {
140 $taxonomy__and_queries[] = $taxonomy;
141 }
142 }
143
144 // Deduplicate within each query type so the same taxonomy requested multiple times with
145 // the same query type is counted with a single query. The "or" and "and" query types are
146 // counted independently and are not merged across types: the "or" branch removes the active
147 // attribute filter before counting while the "and" branch keeps it, so for the same taxonomy
148 // the two counts can legitimately differ.
149 $taxonomy__or_queries = array_unique( $taxonomy__or_queries );
150 $taxonomy__and_queries = array_unique( $taxonomy__and_queries );
151
152 $data['attribute_counts'] = [];
153 // Or type queries need special handling because the attribute, if set, needs removing from the query first otherwise counts would not be correct.
154 if ( $taxonomy__or_queries ) {
155 foreach ( $taxonomy__or_queries as $taxonomy ) {
156 $filter_request = clone $request;
157 $filter_attributes = $filter_request->get_param( 'attributes' );
158
159 if ( ! empty( $filter_attributes ) ) {
160 $filter_attributes = array_filter(
161 $filter_attributes,
162 function ( $query ) use ( $taxonomy ) {
163 // $taxonomy is already sanitized, so sanitize the active attribute too for a like-for-like comparison.
164 return wc_sanitize_taxonomy_name( $query['attribute'] ) !== $taxonomy;
165 }
166 );
167 }
168
169 $filter_request->set_param( 'attributes', $filter_attributes );
170 $counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] );
171
172 foreach ( $counts as $key => $value ) {
173 $data['attribute_counts'][] = (object) [
174 'term' => $key,
175 'count' => $value,
176 ];
177 }
178 }
179 }
180
181 if ( $taxonomy__and_queries ) {
182 $counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries );
183
184 foreach ( $counts as $key => $value ) {
185 $data['attribute_counts'][] = (object) [
186 'term' => $key,
187 'count' => $value,
188 ];
189 }
190 }
191 }
192
193 if ( ! empty( $request['calculate_rating_counts'] ) ) {
194 $filter_request = clone $request;
195 $counts = $filters->get_rating_counts( $filter_request );
196 $data['rating_counts'] = [];
197
198 foreach ( $counts as $key => $value ) {
199 $data['rating_counts'][] = (object) [
200 'rating' => $key,
201 'count' => $value,
202 ];
203 }
204 }
205
206 if ( ! empty( $request['calculate_taxonomy_counts'] ) ) {
207 // Normalize to the canonical taxonomy name before deduping so textual variants
208 // (e.g. differing case or surrounding whitespace) collapse to a single query, and keep
209 // only registered taxonomies so non-existent ones do not trigger wasted queries.
210 $taxonomies = array_unique( array_filter( array_map( 'wc_sanitize_taxonomy_name', $request['calculate_taxonomy_counts'] ), 'taxonomy_exists' ) );
211 $data['taxonomy_counts'] = [];
212
213 if ( $taxonomies ) {
214 $counts = $filters->get_taxonomy_counts( $request, $taxonomies );
215
216 foreach ( $counts as $key => $value ) {
217 $data['taxonomy_counts'][] = (object) [
218 'term' => $key,
219 'count' => $value,
220 ];
221 }
222 }
223 }
224
225 return rest_ensure_response( $this->schema->get_item_response( $data ) );
226 }
227
228 /**
229 * Get the query params for collections of products.
230 *
231 * @return array
232 */
233 public function get_collection_params() {
234 $params = ( new Products( $this->schema_controller, $this->schema ) )->get_collection_params();
235
236 $params['calculate_price_range'] = [
237 'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'woocommerce' ),
238 'type' => 'boolean',
239 'default' => false,
240 ];
241
242 $params['calculate_stock_status_counts'] = [
243 'description' => __( 'If true, calculates stock counts for products in the collection.', 'woocommerce' ),
244 'type' => 'boolean',
245 'default' => false,
246 ];
247
248 $params['calculate_attribute_counts'] = [
249 'description' => __( 'If requested, calculates attribute term counts for products in the collection.', 'woocommerce' ),
250 'type' => 'array',
251 'items' => [
252 'type' => 'object',
253 'properties' => [
254 'taxonomy' => [
255 'description' => __( 'Taxonomy name.', 'woocommerce' ),
256 'type' => 'string',
257 'context' => [ 'view', 'edit' ],
258 'readonly' => true,
259 ],
260 'query_type' => [
261 'description' => __( 'Filter condition being performed which may affect counts. Valid values include "and" and "or".', 'woocommerce' ),
262 'type' => 'string',
263 'enum' => [ 'and', 'or' ],
264 'context' => [ 'view', 'edit' ],
265 'readonly' => true,
266 ],
267 ],
268 ],
269 'default' => [],
270 'maxItems' => self::COUNTS_MAX_ITEMS,
271 ];
272
273 $params['calculate_rating_counts'] = [
274 'description' => __( 'If true, calculates rating counts for products in the collection.', 'woocommerce' ),
275 'type' => 'boolean',
276 'default' => false,
277 ];
278
279 $params['calculate_taxonomy_counts'] = [
280 'description' => __( 'If requested, calculates taxonomy term counts for products in the collection.', 'woocommerce' ),
281 'type' => 'array',
282 'items' => [
283 'type' => 'string',
284 'description' => __( 'Taxonomy name.', 'woocommerce' ),
285 ],
286 'default' => [],
287 'maxItems' => self::COUNTS_MAX_ITEMS,
288 ];
289
290 return $params;
291 }
292 }
293