2019-10-25 09:43:52 +00:00
|
|
|
<?php
|
2020-04-22 14:39:19 +00:00
|
|
|
namespace Automattic\WooCommerce\Blocks\StoreApi\Routes;
|
2019-10-25 09:43:52 +00:00
|
|
|
|
2020-04-22 14:39:19 +00:00
|
|
|
use Automattic\WooCommerce\Blocks\StoreApi\Utilities\ProductQueryFilters;
|
2019-10-25 09:43:52 +00:00
|
|
|
|
|
|
|
/**
|
2020-03-13 12:41:01 +00:00
|
|
|
* ProductCollectionData route.
|
2020-09-07 18:01:07 +00:00
|
|
|
* Get aggregate data from a collection of products.
|
|
|
|
*
|
|
|
|
* Supports the same parameters as /products, but returns a different response.
|
|
|
|
*
|
|
|
|
* @internal This API is used internally by Blocks--it is still in flux and may be subject to revisions.
|
2019-10-25 09:43:52 +00:00
|
|
|
*/
|
2020-03-13 12:41:01 +00:00
|
|
|
class ProductCollectionData extends AbstractRoute {
|
2019-10-25 09:43:52 +00:00
|
|
|
/**
|
2020-03-13 12:41:01 +00:00
|
|
|
* Get the path of this REST route.
|
2019-10-25 09:43:52 +00:00
|
|
|
*
|
2020-03-13 12:41:01 +00:00
|
|
|
* @return string
|
2019-10-25 09:43:52 +00:00
|
|
|
*/
|
2020-03-13 12:41:01 +00:00
|
|
|
public function get_path() {
|
|
|
|
return '/products/collection-data';
|
2019-12-18 11:29:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2020-03-13 12:41:01 +00:00
|
|
|
* Get method arguments for this REST route.
|
2019-12-18 11:29:20 +00:00
|
|
|
*
|
2020-03-13 12:41:01 +00:00
|
|
|
* @return array An array of endpoints.
|
2019-12-18 11:29:20 +00:00
|
|
|
*/
|
2020-03-13 12:41:01 +00:00
|
|
|
public function get_args() {
|
|
|
|
return [
|
|
|
|
[
|
2020-07-29 08:39:54 +00:00
|
|
|
'methods' => \WP_REST_Server::READABLE,
|
|
|
|
'callback' => [ $this, 'get_response' ],
|
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
'args' => $this->get_collection_params(),
|
2020-03-13 12:41:01 +00:00
|
|
|
],
|
|
|
|
'schema' => [ $this->schema, 'get_public_item_schema' ],
|
|
|
|
];
|
2019-10-25 09:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a collection of posts and add the post title filter option to \WP_Query.
|
|
|
|
*
|
2020-03-13 12:41:01 +00:00
|
|
|
* @throws RouteException On error.
|
|
|
|
* @param \WP_REST_Request $request Request object.
|
|
|
|
* @return \WP_REST_Response
|
2019-10-25 09:43:52 +00:00
|
|
|
*/
|
2020-03-13 12:41:01 +00:00
|
|
|
protected function get_route_response( \WP_REST_Request $request ) {
|
2019-12-18 11:29:20 +00:00
|
|
|
$data = [
|
2021-08-20 15:56:14 +00:00
|
|
|
'min_price' => null,
|
|
|
|
'max_price' => null,
|
|
|
|
'attribute_counts' => null,
|
|
|
|
'stock_status_counts' => null,
|
|
|
|
'rating_counts' => null,
|
2019-10-25 09:43:52 +00:00
|
|
|
];
|
|
|
|
$filters = new ProductQueryFilters();
|
|
|
|
|
|
|
|
if ( ! empty( $request['calculate_price_range'] ) ) {
|
2019-11-23 17:04:30 +00:00
|
|
|
$filter_request = clone $request;
|
|
|
|
$filter_request->set_param( 'min_price', null );
|
|
|
|
$filter_request->set_param( 'max_price', null );
|
2019-10-25 09:43:52 +00:00
|
|
|
|
2019-12-18 11:29:20 +00:00
|
|
|
$price_results = $filters->get_filtered_price( $filter_request );
|
|
|
|
$data['min_price'] = $price_results->min_price;
|
|
|
|
$data['max_price'] = $price_results->max_price;
|
2019-10-25 09:43:52 +00:00
|
|
|
}
|
|
|
|
|
2021-08-20 15:56:14 +00:00
|
|
|
if ( ! empty( $request['calculate_stock_status_counts'] ) ) {
|
|
|
|
$filter_request = clone $request;
|
|
|
|
$counts = $filters->get_stock_status_counts( $filter_request );
|
|
|
|
|
|
|
|
$data['stock_status_counts'] = [];
|
|
|
|
|
|
|
|
foreach ( $counts as $key => $value ) {
|
|
|
|
$data['stock_status_counts'][] = (object) [
|
|
|
|
'status' => $key,
|
|
|
|
'count' => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-25 09:43:52 +00:00
|
|
|
if ( ! empty( $request['calculate_attribute_counts'] ) ) {
|
2019-11-23 17:04:30 +00:00
|
|
|
$taxonomy__or_queries = [];
|
|
|
|
$taxonomy__and_queries = [];
|
|
|
|
|
|
|
|
foreach ( $request['calculate_attribute_counts'] as $attributes_to_count ) {
|
2020-06-16 08:58:08 +00:00
|
|
|
if ( ! empty( $attributes_to_count['taxonomy'] ) ) {
|
|
|
|
if ( empty( $attributes_to_count['query_type'] ) || 'or' === $attributes_to_count['query_type'] ) {
|
|
|
|
$taxonomy__or_queries[] = $attributes_to_count['taxonomy'];
|
|
|
|
} else {
|
|
|
|
$taxonomy__and_queries[] = $attributes_to_count['taxonomy'];
|
|
|
|
}
|
2019-11-23 17:04:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 11:29:20 +00:00
|
|
|
$data['attribute_counts'] = [];
|
2019-11-23 17:04:30 +00:00
|
|
|
// Or type queries need special handling because the attribute, if set, needs removing from the query first otherwise counts would not be correct.
|
|
|
|
if ( $taxonomy__or_queries ) {
|
|
|
|
foreach ( $taxonomy__or_queries as $taxonomy ) {
|
|
|
|
$filter_request = clone $request;
|
|
|
|
$filter_attributes = $filter_request->get_param( 'attributes' );
|
|
|
|
|
|
|
|
if ( ! empty( $filter_attributes ) ) {
|
|
|
|
$filter_attributes = array_filter(
|
|
|
|
$filter_attributes,
|
2020-06-16 08:58:19 +00:00
|
|
|
function( $query ) use ( $taxonomy ) {
|
2019-11-23 17:04:30 +00:00
|
|
|
return $query['attribute'] !== $taxonomy;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
$filter_request->set_param( 'attributes', $filter_attributes );
|
|
|
|
$counts = $filters->get_attribute_counts( $filter_request, [ $taxonomy ] );
|
|
|
|
|
|
|
|
foreach ( $counts as $key => $value ) {
|
2020-01-17 11:34:15 +00:00
|
|
|
$data['attribute_counts'][] = (object) [
|
2019-11-23 17:04:30 +00:00
|
|
|
'term' => $key,
|
|
|
|
'count' => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $taxonomy__and_queries ) {
|
|
|
|
$counts = $filters->get_attribute_counts( $request, $taxonomy__and_queries );
|
|
|
|
|
|
|
|
foreach ( $counts as $key => $value ) {
|
2020-01-17 11:34:15 +00:00
|
|
|
$data['attribute_counts'][] = (object) [
|
2019-11-23 17:04:30 +00:00
|
|
|
'term' => $key,
|
|
|
|
'count' => $value,
|
|
|
|
];
|
|
|
|
}
|
2019-10-25 09:43:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( ! empty( $request['calculate_rating_counts'] ) ) {
|
2019-12-18 11:29:20 +00:00
|
|
|
$filter_request = clone $request;
|
|
|
|
$counts = $filters->get_rating_counts( $filter_request );
|
|
|
|
$data['rating_counts'] = [];
|
2019-10-25 09:43:52 +00:00
|
|
|
|
|
|
|
foreach ( $counts as $key => $value ) {
|
2020-01-17 11:34:15 +00:00
|
|
|
$data['rating_counts'][] = (object) [
|
2019-10-25 09:43:52 +00:00
|
|
|
'rating' => $key,
|
|
|
|
'count' => $value,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-13 12:41:01 +00:00
|
|
|
return rest_ensure_response( $this->schema->get_item_response( $data ) );
|
2019-10-25 09:43:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the query params for collections of products.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_collection_params() {
|
2020-03-13 12:41:01 +00:00
|
|
|
$params = ( new Products( $this->schema ) )->get_collection_params();
|
2019-10-25 09:43:52 +00:00
|
|
|
|
2019-12-18 11:29:20 +00:00
|
|
|
$params['calculate_price_range'] = [
|
2019-10-25 09:43:52 +00:00
|
|
|
'description' => __( 'If true, calculates the minimum and maximum product prices for the collection.', 'woo-gutenberg-products-block' ),
|
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
2019-12-18 11:29:20 +00:00
|
|
|
];
|
2019-10-25 09:43:52 +00:00
|
|
|
|
2021-08-20 15:56:14 +00:00
|
|
|
$params['calculate_stock_status_counts'] = [
|
|
|
|
'description' => __( 'If true, calculates stock counts for products in the collection.', 'woo-gutenberg-products-block' ),
|
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
|
|
|
];
|
|
|
|
|
2019-12-18 11:29:20 +00:00
|
|
|
$params['calculate_attribute_counts'] = [
|
2019-10-25 09:43:52 +00:00
|
|
|
'description' => __( 'If requested, calculates attribute term counts for products in the collection.', 'woo-gutenberg-products-block' ),
|
|
|
|
'type' => 'array',
|
2019-12-18 11:29:20 +00:00
|
|
|
'items' => [
|
2019-11-23 17:04:30 +00:00
|
|
|
'type' => 'object',
|
2019-12-18 11:29:20 +00:00
|
|
|
'properties' => [
|
|
|
|
'taxonomy' => [
|
2019-11-23 17:04:30 +00:00
|
|
|
'description' => __( 'Taxonomy name.', 'woo-gutenberg-products-block' ),
|
|
|
|
'type' => 'string',
|
2019-12-18 11:29:20 +00:00
|
|
|
'context' => [ 'view', 'edit' ],
|
2019-11-23 17:04:30 +00:00
|
|
|
'readonly' => true,
|
2019-12-18 11:29:20 +00:00
|
|
|
],
|
|
|
|
'query_type' => [
|
2019-11-23 17:04:30 +00:00
|
|
|
'description' => __( 'Query type being performed which may affect counts. Valid values include "and" and "or".', 'woo-gutenberg-products-block' ),
|
|
|
|
'type' => 'string',
|
2019-12-18 11:29:20 +00:00
|
|
|
'enum' => [ 'and', 'or' ],
|
|
|
|
'context' => [ 'view', 'edit' ],
|
2019-11-23 17:04:30 +00:00
|
|
|
'readonly' => true,
|
2019-12-18 11:29:20 +00:00
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'default' => [],
|
|
|
|
];
|
2019-10-25 09:43:52 +00:00
|
|
|
|
2019-12-18 11:29:20 +00:00
|
|
|
$params['calculate_rating_counts'] = [
|
2019-10-25 09:43:52 +00:00
|
|
|
'description' => __( 'If true, calculates rating counts for products in the collection.', 'woo-gutenberg-products-block' ),
|
|
|
|
'type' => 'boolean',
|
|
|
|
'default' => false,
|
2019-12-18 11:29:20 +00:00
|
|
|
];
|
2019-10-25 09:43:52 +00:00
|
|
|
|
|
|
|
return $params;
|
|
|
|
}
|
|
|
|
}
|