Automattic\WooCommerce\StoreApi\Routes\V1\Ucp
CatalogProduct::get_route_post_response
Handle the request.
Метод класса: CatalogProduct{}
Хуков нет.
Возвращает
\WP_REST_Response.
Использование
// protected - в коде основоного (родительского) или дочернего класса $result = $this->get_route_post_response( $request );
- $request(WP_REST_Request) (обязательный)
- Request object.
Код CatalogProduct::get_route_post_response() CatalogProduct::get route post response WC 11.3.0
protected function get_route_post_response( \WP_REST_Request $request ) {
$id = trim( strval( $request->get_param( 'id' ) ) );
if ( '' === $id ) {
return $this->ucp_error( 'VALIDATION_ERROR', __( 'id is required.', 'woocommerce' ), 'recoverable', 400 );
}
$selected = (array) ( $request->get_param( 'selected' ) ?? array() );
// Checked before the product is loaded: an over-long selection is refused
// without paying for the lookup it would have been applied to.
if ( count( $selected ) > self::MAX_SELECTED_OPTIONS ) {
return $this->ucp_error(
'request_too_large',
sprintf(
/* translators: %d: maximum number of selected options accepted per request. */
__( 'selected accepts at most %d options per request.', 'woocommerce' ),
self::MAX_SELECTED_OPTIONS
),
'recoverable',
400
);
}
$resolved = $this->resolve_product( $id );
if ( null === $resolved ) {
// A missing product is reported in the envelope, not as a 404: UCP
// clients read `ucp.status` and `messages`, not the HTTP status.
return new \WP_REST_Response(
array(
'ucp' => $this->ucp_metadata( 'error' ),
'messages' => array(
array(
'type' => 'error',
'code' => 'not_found',
'content' => __( 'Product not found.', 'woocommerce' ),
'severity' => 'unrecoverable',
'path' => '$.id',
'content_type' => 'plain',
),
),
),
200
);
}
$this->mapper()->begin_response();
$product = $this->mapper()->map_detail_product( $resolved['product'], $this->normalize_selected( $selected ), (int) $resolved['variation_id'] );
$response = array(
'ucp' => $this->ucp_metadata( 'success' ),
'product' => $product,
);
$messages = $this->truncation_messages( $this->mapper()->take_truncations(), array( $product ), '$.product' );
$currency_warning = $this->currency_warning( $this->get_context( $request ), $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 );
}