Automattic\WooCommerce\StoreApi\Routes\V1\Agentic
CheckoutSessionsUpdate::get_route_post_response
Handle the request and return a valid response for this endpoint.
Method of the class: CheckoutSessionsUpdate{}
No Hooks.
Returns
\WP_REST_Response|\WP_Error.
Usage
// protected - for code of main (parent) or child class $result = $this->get_route_post_response( $request );
- $request(WP_REST_Request) (required)
- Request object.
CheckoutSessionsUpdate::get_route_post_response() CheckoutSessionsUpdate::get route post response code WC 10.7.0
protected function get_route_post_response( \WP_REST_Request $request ) {
$cart = $this->cart_controller->get_cart_instance();
$checkout_session = new AgenticCheckoutSession( $cart );
$current_status = AgenticCheckoutUtils::calculate_status( $checkout_session );
if ( ! in_array( $current_status, CheckoutSessionStatus::ALLOWED_STATUSES_FOR_UPDATE, true ) ) {
$allowed_statuses = implode( ', ', CheckoutSessionStatus::ALLOWED_STATUSES_FOR_UPDATE );
$message = sprintf(
/* translators: 1: current session status, 2: allowed statuses */
__( 'Checkout session cannot be updated. Current status: %1$s. Allowed statuses: %2$s', 'woocommerce' ),
$current_status,
$allowed_statuses
);
return Error::invalid_request( ErrorCode::INVALID, $message )->to_rest_response();
}
// Update items if provided.
$items = $request->get_param( 'items' );
if ( null !== $items ) {
// Clear existing cart items and replace with new ones.
$this->cart_controller->empty_cart();
$error = AgenticCheckoutUtils::add_items_to_cart(
$items,
$this->cart_controller,
$checkout_session->get_messages()
);
if ( $error instanceof Error ) {
return $error->to_rest_response();
}
}
// Update buyer information if provided.
$buyer = $request->get_param( 'buyer' );
if ( null !== $buyer ) {
AgenticCheckoutUtils::set_buyer_data( $buyer, WC()->customer );
}
// Update fulfillment address if provided.
$address = $request->get_param( 'fulfillment_address' );
if ( null !== $address ) {
AgenticCheckoutUtils::set_fulfillment_address( $address, WC()->customer );
}
// Update selected shipping method if provided.
$fulfillment_option_id = $request->get_param( 'fulfillment_option_id' );
if ( null !== $fulfillment_option_id ) {
$option_id = wc_clean( (string) $fulfillment_option_id );
$packages = WC()->shipping()->get_packages();
foreach ( $packages as $package ) {
foreach ( (array) ( $package['rates'] ?? array() ) as $rate ) {
if ( $rate->get_id() === $option_id ) {
WC()->session->set( SessionKey::CHOSEN_SHIPPING_METHODS, array( $option_id ) );
break 2;
}
}
}
}
// Calculate totals after all updates.
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 );
}