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
4 months 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
4 months ago
Checkout.php
1 month ago
CheckoutOrder.php
1 year 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
11 months ago
ProductReviews.php
4 months ago
ProductTags.php
1 year ago
Products.php
3 months ago
ProductsById.php
3 months ago
ProductsBySlug.php
3 months 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
253 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 | * Get the path of this REST route. |
| 29 | * |
| 30 | * @return string |
| 31 | */ |
| 32 | public function get_path() { |
| 33 | return self::get_path_regex(); |
| 34 | } |
| 35 | |
| 36 | /** |
| 37 | * Get the path of this rest route. |
| 38 | * |
| 39 | * @return string |
| 40 | */ |
| 41 | public static function get_path_regex() { |
| 42 | return '/products/collection-data'; |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Get method arguments for this REST route. |
| 47 | * |
| 48 | * @return array An array of endpoints. |
| 49 | */ |
| 50 | public function get_args() { |
| 51 | return [ |
| 52 | [ |
| 53 | 'methods' => \WP_REST_Server::READABLE, |
| 54 | 'callback' => [ $this, 'get_response' ], |
| 55 | 'permission_callback' => '__return_true', |
| 56 | 'args' => $this->get_collection_params(), |
| 57 | 'allow_batch' => [ 'v1' => true ], |
| 58 | ], |
| 59 | 'schema' => [ $this->schema, 'get_public_item_schema' ], |
| 60 | ]; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get a collection of posts and add the post title filter option to \WP_Query. |
| 65 | * |
| 66 | * @param \WP_REST_Request $request Request object. |
| 67 | * @return \WP_REST_Response |
| 68 | */ |
| 69 | protected function get_route_response( \WP_REST_Request $request ) { |
| 70 | $data = [ |
| 71 | 'min_price' => null, |
| 72 | 'max_price' => null, |
| 73 | 'attribute_counts' => null, |
| 74 | 'stock_status_counts' => null, |
| 75 | 'rating_counts' => null, |
| 76 | 'taxonomy_counts' => null, |
| 77 | ]; |
| 78 | $filters = new ProductQueryFilters(); |
| 79 | |
| 80 | if ( ! empty( $request['calculate_price_range'] ) ) { |
| 81 | $filter_request = clone $request; |
| 82 | $filter_request->set_param( 'min_price', null ); |
| 83 | $filter_request->set_param( 'max_price', null ); |
| 84 | |
| 85 | $price_results = $filters->get_filtered_price( $filter_request ); |
| 86 | $data['min_price'] = $price_results->min_price; |
| 87 | $data['max_price'] = $price_results->max_price; |
| 88 | } |
| 89 | |
| 90 | if ( ! empty( $request['calculate_stock_status_counts'] ) ) { |
| 91 | $filter_request = clone $request; |
| 92 | $counts = $filters->get_stock_status_counts( $filter_request ); |
| 93 | |
| 94 | $data['stock_status_counts'] = []; |
| 95 | |
| 96 | foreach ( $counts as $key => $value ) { |
| 97 | $data['stock_status_counts'][] = (object) [ |
| 98 | 'status' => $key, |
| 99 | 'count' => $value, |
| 100 | ]; |
| 101 | } |
| 102 | } |
| 103 | |
| 104 | if ( ! empty( $request['calculate_attribute_counts'] ) ) { |
| 105 | $taxonomy__or_queries = []; |
| 106 | $taxonomy__and_queries = []; |
| 107 | |
| 108 | foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) { |
| 109 | if ( ! empty( $attributes_to_count['taxonomy'] ) ) { |
| 110 | if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) { |
| 111 | $taxonomy__or_queries[] = $attributes_to_count['taxonomy']; |
| 112 | } else { |
| 113 | $taxonomy__and_queries[] = $attributes_to_count['taxonomy']; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | $data['attribute_counts'] = []; |
| 119 | // Or type queries need special handling because the attribute, if set, needs removing from the query first otherwise counts would not be correct. |
| 120 | if ( $taxonomy__or_queries ) { |
| 121 | foreach ( $taxonomy__or_queries as $taxonomy ) { |
| 122 | $filter_request = clone $request; |
| 123 | $filter_attributes = $filter_request->get_param( 'attributes' ); |
| 124 | |
| 125 | if ( ! empty( $filter_attributes ) ) { |
| 126 | $filter_attributes = array_filter( |
| 127 | $filter_attributes, |
| 128 | function ( $query ) use ( $taxonomy ) { |
| 129 | return $query['attribute'] !== $taxonomy; |
| 130 | } |
| 131 | ); |
| 132 | } |
| 133 | |
| 134 | $filter_request->set_param( 'attributes', $filter_attributes ); |
| 135 | $counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] ); |
| 136 | |
| 137 | foreach ( $counts as $key => $value ) { |
| 138 | $data['attribute_counts'][] = (object) [ |
| 139 | 'term' => $key, |
| 140 | 'count' => $value, |
| 141 | ]; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | if ( $taxonomy__and_queries ) { |
| 147 | $counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries ); |
| 148 | |
| 149 | foreach ( $counts as $key => $value ) { |
| 150 | $data['attribute_counts'][] = (object) [ |
| 151 | 'term' => $key, |
| 152 | 'count' => $value, |
| 153 | ]; |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | if ( ! empty( $request['calculate_rating_counts'] ) ) { |
| 159 | $filter_request = clone $request; |
| 160 | $counts = $filters->get_rating_counts( $filter_request ); |
| 161 | $data['rating_counts'] = []; |
| 162 | |
| 163 | foreach ( $counts as $key => $value ) { |
| 164 | $data['rating_counts'][] = (object) [ |
| 165 | 'rating' => $key, |
| 166 | 'count' => $value, |
| 167 | ]; |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | if ( ! empty( $request['calculate_taxonomy_counts'] ) ) { |
| 172 | $taxonomies = $request['calculate_taxonomy_counts']; |
| 173 | $data['taxonomy_counts'] = []; |
| 174 | |
| 175 | if ( $taxonomies ) { |
| 176 | $counts = $filters->get_taxonomy_counts( $request, $taxonomies ); |
| 177 | |
| 178 | foreach ( $counts as $key => $value ) { |
| 179 | $data['taxonomy_counts'][] = (object) [ |
| 180 | 'term' => $key, |
| 181 | 'count' => $value, |
| 182 | ]; |
| 183 | } |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | return rest_ensure_response( $this->schema->get_item_response( $data ) ); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | * Get the query params for collections of products. |
| 192 | * |
| 193 | * @return array |
| 194 | */ |
| 195 | public function get_collection_params() { |
| 196 | $params = ( new Products( $this->schema_controller, $this->schema ) )->get_collection_params(); |
| 197 | |
| 198 | $params['calculate_price_range'] = [ |
| 199 | 'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'woocommerce' ), |
| 200 | 'type' => 'boolean', |
| 201 | 'default' => false, |
| 202 | ]; |
| 203 | |
| 204 | $params['calculate_stock_status_counts'] = [ |
| 205 | 'description' => __( 'If true, calculates stock counts for products in the collection.', 'woocommerce' ), |
| 206 | 'type' => 'boolean', |
| 207 | 'default' => false, |
| 208 | ]; |
| 209 | |
| 210 | $params['calculate_attribute_counts'] = [ |
| 211 | 'description' => __( 'If requested, calculates attribute term counts for products in the collection.', 'woocommerce' ), |
| 212 | 'type' => 'array', |
| 213 | 'items' => [ |
| 214 | 'type' => 'object', |
| 215 | 'properties' => [ |
| 216 | 'taxonomy' => [ |
| 217 | 'description' => __( 'Taxonomy name.', 'woocommerce' ), |
| 218 | 'type' => 'string', |
| 219 | 'context' => [ 'view', 'edit' ], |
| 220 | 'readonly' => true, |
| 221 | ], |
| 222 | 'query_type' => [ |
| 223 | 'description' => __( 'Filter condition being performed which may affect counts. Valid values include "and" and "or".', 'woocommerce' ), |
| 224 | 'type' => 'string', |
| 225 | 'enum' => [ 'and', 'or' ], |
| 226 | 'context' => [ 'view', 'edit' ], |
| 227 | 'readonly' => true, |
| 228 | ], |
| 229 | ], |
| 230 | ], |
| 231 | 'default' => [], |
| 232 | ]; |
| 233 | |
| 234 | $params['calculate_rating_counts'] = [ |
| 235 | 'description' => __( 'If true, calculates rating counts for products in the collection.', 'woocommerce' ), |
| 236 | 'type' => 'boolean', |
| 237 | 'default' => false, |
| 238 | ]; |
| 239 | |
| 240 | $params['calculate_taxonomy_counts'] = [ |
| 241 | 'description' => __( 'If requested, calculates taxonomy term counts for products in the collection.', 'woocommerce' ), |
| 242 | 'type' => 'array', |
| 243 | 'items' => [ |
| 244 | 'type' => 'string', |
| 245 | 'description' => __( 'Taxonomy name.', 'woocommerce' ), |
| 246 | ], |
| 247 | 'default' => [], |
| 248 | ]; |
| 249 | |
| 250 | return $params; |
| 251 | } |
| 252 | } |
| 253 |