Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::get_response │ public │ WC 1.0

Get the route response based on the type of request.

Метод класса: Checkout{}

Хуков нет.

Возвращает

\WP_REST_Response.

Использование

$Checkout = new Checkout();
$Checkout->get_response( $request );
$request(WP_REST_Request) (обязательный)
Request object.

Код Checkout::get_response() WC 11.3.0

public function get_response( \WP_REST_Request $request ) {
	$this->reset_request_state();

	try {
		$this->load_cart_session( $request );
	} catch ( \Throwable $error ) {
		return $this->add_response_headers( $this->get_cart_session_error_response( $error ) );
	}

	$response    = null;
	$nonce_check = $this->requires_nonce( $request ) ? $this->check_nonce( $request ) : null;

	if ( is_wp_error( $nonce_check ) ) {
		$response = $nonce_check;
	}

	if ( ! $response ) {
		try {
			$response = $this->get_response_by_request_method( $request );
		} catch ( InvalidCartException $error ) {
			$response = $this->get_route_error_response_from_object( $error->getError(), $error->getCode(), $error->getAdditionalData() );
		} catch ( RouteException $error ) {
			$response = $this->get_route_error_response( $error->getErrorCode(), $error->getMessage(), $error->getCode(), $error->getAdditionalData() );
		} catch ( \Exception $error ) {
			$response = $this->get_route_error_response( 'woocommerce_rest_unknown_server_error', $error->getMessage(), 500 );
		} catch ( \Throwable $error ) {
			$response = UnexpectedErrorResponse::create( $error, static::class );
		}
	}

	if ( is_wp_error( $response ) ) {
		$response = $this->error_to_response( $response );

		// If we encountered an exception, free up stock and release held coupons. Only an order
		// that moved past awaiting payment keeps both: its stock is committed and its coupons
		// are spent. Everything else still needs the cleanup, a fully discounted order and one
		// that never left checkout-draft included.
		// Re-read the order first, since a gateway that advanced it may have done so on its own
		// instance, leaving the one held here reporting a stale status.
		$order = $this->order ? wc_get_order( $this->order->get_id() ) : null;
		$order = $order instanceof \WC_Order ? $order : $this->order;

		if ( $order && ! $this->order_moved_past_payment( $order ) ) {
			wc_release_stock_for_order( $order );
			wc_release_coupons_for_order( $order );
		}

		if ( $request->get_method() === \WP_REST_Server::CREATABLE ) {
			// Step logs the exception. If nothing abnormal occurred during the place order POST request, flow the log is removed.
			wc_log_order_step(
				'[Store API #FAIL] Placing Order failed',
				array(
					'status' => $response->get_status(),
					'data'   => $response->get_data(),
				),
				true
			);
		}
	}

	return $this->add_response_headers( $response );
}