Automattic\WooCommerce\Blocks\BlockTypes

SavedForLater::prefetch_itemsprivateWC 1.0

Prefetch the saved-for-later items via rest_do_request(). Logged-out users short-circuit to an empty list — the route requires authentication and we don't want to fire an API call that's only going to 401.

Method of the class: SavedForLater{}

No Hooks.

Returns

Array. array<string, mixed>> Items in the schema response shape.

Usage

// private - for code of main (parent) class only
$result = $this->prefetch_items(): array;

SavedForLater::prefetch_items() code WC 10.9.4

private function prefetch_items(): array {
	if ( ! is_user_logged_in() ) {
		return array();
	}

	$request  = new \WP_REST_Request( 'GET', '/wc/store/v1/shopper-lists/' . self::LIST_SLUG . '/items' );
	$response = rest_do_request( $request );

	if ( $response->is_error() ) {
		$error   = $response->as_error();
		$message = $error instanceof \WP_Error ? $error->get_error_message() : 'Unknown error';
		// Logged at debug level on purpose: prefetch failures are
		// often transient (network blips, auth refresh races) and
		// the user-visible behaviour is the empty state — nothing
		// for ops to act on. Anyone investigating a regression can
		// flip the WC logger to debug to surface them.
		wc_get_logger()->debug(
			sprintf( 'Saved for Later prefetch failed: %s', $message ),
			array(
				'source' => 'saved-for-later',
				'data'   => array( 'slug' => self::LIST_SLUG ),
			)
		);
		return array();
	}

	$data = $response->get_data();
	if ( ! is_array( $data ) && ! is_object( $data ) ) {
		return array();
	}

	// The schema casts `prices` and image entries to stdClass so the
	// JSON response renders objects, not arrays. Round-trip through
	// JSON encode/decode to normalise everything to nested arrays so
	// the SSR markup helpers below can treat fields uniformly.
	$decoded = json_decode( (string) wp_json_encode( $data ), true );
	return is_array( $decoded ) ? $decoded : array();
}