Automattic\WooCommerce\StoreApi\Routes\V1\Agentic

CheckoutSessions::get_route_post_responseprotectedWC 1.0

Handle the request and return a valid response for this endpoint.

Method of the class: CheckoutSessions{}

No Hooks.

Returns

\WP_REST_Response.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_route_post_response( $request );
$request(WP_REST_Request) (required)
Request object.

CheckoutSessions::get_route_post_response() code WC 10.8.1

protected function get_route_post_response( \WP_REST_Request $request ) {
	$checkout_session = new AgenticCheckoutSession( $this->cart_controller->get_cart_instance() );

	// Clear existing cart to start fresh for POST requests.
	$this->cart_controller->empty_cart();

	// Add items to cart.
	$items = $request->get_param( 'items' );
	$error = AgenticCheckoutUtils::add_items_to_cart( $items, $this->cart_controller, $checkout_session->get_messages() );
	// Halt for critical errors.
	if ( $error instanceof Error ) {
		return $error->to_rest_response();
	}

	// Set buyer information.
	$buyer = $request->get_param( 'buyer' );
	if ( $buyer ) {
		AgenticCheckoutUtils::set_buyer_data( $buyer, WC()->customer );
	}

	// Set fulfillment address.
	$address = $request->get_param( 'fulfillment_address' );
	if ( $address ) {
		AgenticCheckoutUtils::set_fulfillment_address( $address, WC()->customer );
	} else {
		// Clear address when not provided (POST creates fresh session).
		AgenticCheckoutUtils::clear_fulfillment_address( WC()->customer );
	}

	// Calculate totals.
	try {
		$this->cart_controller->calculate_totals();
	} catch ( \Exception $e ) {
		$message = wp_specialchars_decode( $e->getMessage(), ENT_QUOTES );
		return Error::processing_error( 'totals_calculation_error', $message )->to_rest_response();
	}

	// Build response from canonical cart schema.
	$response = $this->schema->get_item_response( $checkout_session );

	// Add protocol headers.
	return AgenticCheckoutUtils::add_protocol_headers( rest_ensure_response( $response ), $request );
}