Automattic\WooCommerce\StoreApi\Routes\V1\Ucp
CatalogSearch::get_route_post_response
Handle the request.
Метод класса: CatalogSearch{}
Хуков нет.
Возвращает
\WP_REST_Response.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->get_route_post_response( $request );
- $request(WP_REST_Request) (обязательный)
- Request object.
Код CatalogSearch::get_route_post_response() CatalogSearch::get route post response WC 11.3.0
protected function get_route_post_response( \WP_REST_Request $request ) {
$query = trim( (string) $request->get_param( 'query' ) );
$filters = (array) ( $request->get_param( 'filters' ) ?? array() );
$pagination = (array) ( $request->get_param( 'pagination' ) ?? array() );
$context = $this->get_context( $request );
$invalid = $this->validate_search_input( $query, $filters );
if ( null !== $invalid ) {
return $invalid;
}
$limit = isset( $pagination['limit'] ) ? min( max( (int) $pagination['limit'], 1 ), 100 ) : 10;
$offset = $this->decode_cursor( isset( $pagination['cursor'] ) ? (string) $pagination['cursor'] : '' );
$category_slugs = null;
if ( ! empty( $filters['categories'] ) && is_array( $filters['categories'] ) ) {
$category_slugs = $this->resolve_category_slugs( $filters['categories'] );
}
// A category list naming no existing term can match nothing; querying
// without the filter would return the whole catalog instead.
if ( null !== $category_slugs && empty( $category_slugs ) ) {
$results = array(
'objects' => array(),
'total' => 0,
);
} else {
$results = $this->query_products( $this->store_api_params( $query, $filters, $category_slugs, $limit, $offset ) );
}
$mapped = $this->mapper()->map_products( $results['objects'] );
$pagination_result = array( 'has_next_page' => $offset + $limit < $results['total'] );
if ( $pagination_result['has_next_page'] ) {
$pagination_result['cursor'] = $this->encode_cursor( $offset + $limit );
}
$response = array(
'ucp' => $this->ucp_metadata( 'success' ),
'products' => $mapped,
'pagination' => $pagination_result,
);
$messages = $this->truncation_messages( $this->mapper()->take_truncations(), $mapped, '$.products' );
$currency_warning = $this->currency_warning( $context, $this->store_currency() );
if ( null !== $currency_warning ) {
array_unshift( $messages, $currency_warning );
}
if ( ! empty( $messages ) ) {
$response['messages'] = $messages;
}
return new \WP_REST_Response( $response, 200 );
}