WooCommerce Code Reference

CartTokenUtils
in package

Cart token utility functions.

Table of Contents

get_cart_link_customer_id()  : string
Get the customer ID from a token passed in the `session` query arg.
get_cart_link_token()  : string
Generate a cart link token.
get_cart_token()  : string
Generate a cart token.
get_cart_token_expiration()  : int
Gets the expiration of the cart token. Defaults to 48h.
get_cart_token_payload()  : array<string|int, mixed>
Get the cart token payload.
get_request_cart_token()  : string
Get the cart token sent with the current HTTP request.
validate_cart_token()  : bool
Validate the cart token.
get_cart_token_secret()  : string
Get the cart token secret.
validate_token()  : bool
Validate a token's signature and check it was issued by one of the given issuers.

Methods

Get the customer ID from a token passed in the `session` query arg.

public static get_cart_link_customer_id(string $token) : string

Accepts cart link tokens and cart tokens, so integrations that pass a cart token in the query arg keep working.

Parameters
$token : string

The cart link token or cart token.

Tags
since
11.4.0
Return values
string — The customer ID, or an empty string if the token is not valid.

Generate a cart link token.

public static get_cart_link_token(string $customer_id) : string

Unlike a cart token, a link token can't be used as a Store API Cart-Token. It only lets another browser copy the cart through the session query arg, without the customer's details.

Parameters
$customer_id : string

The customer ID.

Tags
since
11.4.0
Return values
string —

get_cart_token()

Generate a cart token.

public static get_cart_token(string $customer_id) : string
Parameters
$customer_id : string

The customer ID.

Return values
string —

get_cart_token_expiration()

Gets the expiration of the cart token. Defaults to 48h.

public static get_cart_token_expiration() : int
Tags
since
11.1.0

Made public.

Return values
int —

get_cart_token_payload()

Get the cart token payload.

public static get_cart_token_payload(string $cart_token) : array<string|int, mixed>

Returns an empty payload unless the signature validates.

Parameters
$cart_token : string

The cart token.

Tags
since
11.1.0

Returns an empty payload for tokens that fail signature validation.

Return values
array<string|int, mixed> —

get_request_cart_token()

Get the cart token sent with the current HTTP request.

public static get_request_cart_token() : string
Tags
since
11.1.0
Return values
string —

validate_cart_token()

Validate the cart token.

public static validate_cart_token(string $cart_token) : bool
Parameters
$cart_token : string

The cart token.

Tags
since
11.4.0

Rejects tokens not issued by the Store API, such as cart link tokens.

Return values
bool —

validate_token()

Validate a token's signature and check it was issued by one of the given issuers.

private static validate_token(string $token, array<string|int, string> $issuers) : bool
Parameters
$token : string

The token.

$issuers : array<string|int, string>

Accepted iss claims.

Return values
bool —