Automattic\WooCommerce\StoreApi\Routes\V1

Checkout::get_route_update_responseprotectedWC 1.0

Get route response for PUT/PATCH requests.

Branches on whether a pending/failed order already exists in the customer's session:

  • Order in session (failed-payment retry): update the existing order via create_or_update_draft_order() + update_order_from_request(). Same shape as the POST flow.
  • No order in session (fresh checkout form interaction): persist request state to the customer session via update_session_from_request() and return a no-order response built from cart + customer + request.

Draft order creation is deferred to POST (place-order time) to avoid orphaned wc-checkout-draft rows from form interactions that never complete. POSTs do not flow through this method — see get_route_post_response().

Method of the class: Checkout{}

Returns

\WP_REST_Response|\WP_Error.

Usage

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

Checkout::get_route_update_response() code WC 10.9.1

protected function get_route_update_response( \WP_REST_Request $request ) {
	$validation_callback = $this->validate_callback( $request );

	if ( is_wp_error( $validation_callback ) ) {
		return $validation_callback;
	}

	/**
	 * Create (or update) Draft Order and process request data.
	 */
	$this->order = $this->get_draft_order();

	if ( $this->order ) {
		$this->create_or_update_draft_order( $request );
		// Order save-point: 1.

		/**
		 * Persist additional fields, order notes and payment method for order.
		 */
		$this->update_order_from_request( $request );
		// Order save-point: 2.
	} else {
		$this->update_session_from_request( $request );
	}

	if ( $request->get_param( '__experimental_calc_totals' ) ) {
		/**
		 * Before triggering validation, ensure totals are current and in turn, things such as shipping costs are present.
		 * This is so plugins that validate other cart data (e.g. conditional shipping and payments) can access this data.
		 */
		$this->cart_controller->calculate_totals();
		/**
		 * Validate that the cart is not empty.
		 */
		$this->cart_controller->validate_cart_not_empty();

		/**
		 * Validate items and fix violations before the order is processed.
		 */
		$this->cart_controller->validate_cart();
	}

	if ( $this->order ) {
		return $this->prepare_item_for_response(
			(object) [
				'order' => wc_get_order( $this->order ),
				'cart'  => $this->cart_controller->get_cart_instance(),
			],
			$request
		);
	}

	/**
	 * Fires after a Store API checkout PATCH request has been validated and live
	 * customer/session state has been updated, before the response is returned.
	 *
	 * Hook this action when an extension needs to observe live checkout state — e.g.
	 * abandoned-cart trackers, side-panel previews, conditional shipping or payment
	 * validators, or anything else that needs to react to every customer interaction
	 * with the form.
	 *
	 * No `WC_Order` exists at this point under deferred draft order creation. Read
	 * checkout state from `WC()->cart`, `WC()->customer`, and the supplied
	 * `$request`, and persist any extension-owned state to `WC()->session`. To
	 * apply that state to the real order at place-order time, hook
	 * `woocommerce_store_api_checkout_update_order_meta` or
	 * `woocommerce_store_api_checkout_update_order_from_request` — both fire
	 * against the real, persisted order at POST exactly as they always have.
	 *
	 * @since 10.8.0
	 *
	 * @param \WP_REST_Request $request The current PATCH request.
	 */
	do_action( 'woocommerce_store_api_checkout_update_draft', $request );

	return $this->build_draft_route_response( $request );
}