Automattic\WooCommerce\Blocks\BlockTypes
AddToWishlistButton::prefetch_items
Prefetch the wishlist 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: AddToWishlistButton{}
No Hooks.
Returns
Array
Usage
// private - for code of main (parent) class only $result = $this->prefetch_items(): array;
AddToWishlistButton::prefetch_items() AddToWishlistButton::prefetch items code WC 10.9.1
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';
wc_get_logger()->debug(
sprintf( 'Add to Wishlist button prefetch failed: %s', $message ),
array(
'source' => 'add-to-wishlist-button',
'data' => array( 'slug' => self::LIST_SLUG ),
)
);
return array();
}
$data = $response->get_data();
if ( ! is_array( $data ) && ! is_object( $data ) ) {
return array();
}
$decoded = json_decode( (string) wp_json_encode( $data ), true );
return is_array( $decoded ) ? $decoded : array();
}