WooCommerce Code Reference

ProductCountCache
in package

A class to cache counts for various product statuses.

Table of Contents

$expiration  : int
Default value for the duration of the objects in the cache, in seconds (may not be used depending on the cache engine used WordPress cache implementation).
$cache_prefix  : string
Cache prefix.
decrement()  : int|false
Decrement the cache value for a given product type and status.
flush()  : void
Flush the cache for a given product type and statuses.
get()  : array<string, int>|null
Get the cache value for a given product type and set of statuses.
increment()  : int|false
Increment the cache value for a given product type and status.
is_cached()  : bool
Check if the cache has a value for a given product type and status.
set()  : bool
Set the cache value for a given product type and status.
set_multiple()  : array<string, bool>
Set the cache count value for multiple statuses at once.
ensure_statuses_for_type()  : void
Adds the given statuses to the cached statuses array for the product type if they are not already stored.
get_cache_key()  : string
Get the cache key for a given product type and status.
get_saved_statuses_cache_key()  : string
Get the cache key for saved statuses of the given product type.
get_saved_statuses_for_type()  : array<string|int, string>
Retrieves the list of known statuses by product type. A cached array of statuses is saved per product type for improved backward compatibility with some of the extensions that don't register all statuses they use with WooCommerce.

Properties

$expiration

Default value for the duration of the objects in the cache, in seconds (may not be used depending on the cache engine used WordPress cache implementation).

protected int $expiration = DAY_IN_SECONDS

Methods

decrement()

Decrement the cache value for a given product type and status.

public decrement(string $product_type, string $product_status[, int $offset = 1 ]) : int|false
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$product_status : string

The status of the product.

$offset : int = 1

The amount to decrement by.

Return values
int|false

flush()

Flush the cache for a given product type and statuses.

public flush([string $product_type = 'product' ][, array<string|int, string> $product_statuses = array() ]) : void
Parameters
$product_type : string = 'product'

The post type (e.g. 'product', 'product_variation').

$product_statuses : array<string|int, string> = array()

The statuses to flush. Flushes all known statuses if empty.

Return values
void

get()

Get the cache value for a given product type and set of statuses.

public get(string $product_type[, array<string|int, string> $product_statuses = array() ]) : array<string, int>|null
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$product_statuses : array<string|int, string> = array()

The statuses to retrieve.

Return values
array<string, int>|null

increment()

Increment the cache value for a given product type and status.

public increment(string $product_type, string $product_status[, int $offset = 1 ]) : int|false
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$product_status : string

The status of the product.

$offset : int = 1

The amount to increment by.

Return values
int|false

is_cached()

Check if the cache has a value for a given product type and status.

public is_cached(string $product_type, string $product_status) : bool
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$product_status : string

The status of the product.

Return values
bool

set()

Set the cache value for a given product type and status.

public set(string $product_type, string $product_status, int $value) : bool
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$product_status : string

The status slug of the product.

$value : int

The value to set.

Return values
bool

set_multiple()

Set the cache count value for multiple statuses at once.

public set_multiple(string $product_type, array<string, int> $counts) : array<string, bool>
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$counts : array<string, int>

Counts keyed by status slug (e.g. [ 'publish' => 10, 'draft' => 5 ]).

Return values
array<string, bool>

ensure_statuses_for_type()

Adds the given statuses to the cached statuses array for the product type if they are not already stored.

private ensure_statuses_for_type(string $product_type, array<string|int, string> $product_statuses) : void
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$product_statuses : array<string|int, string>

One or more statuses to add.

Return values
void

get_cache_key()

Get the cache key for a given product type and status.

private get_cache_key(string $product_type, string $product_status) : string
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

$product_status : string

The status of the product.

Return values
string

get_saved_statuses_cache_key()

Get the cache key for saved statuses of the given product type.

private get_saved_statuses_cache_key(string $product_type) : string
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

Return values
string

get_saved_statuses_for_type()

Retrieves the list of known statuses by product type. A cached array of statuses is saved per product type for improved backward compatibility with some of the extensions that don't register all statuses they use with WooCommerce.

private get_saved_statuses_for_type(string $product_type) : array<string|int, string>
Parameters
$product_type : string

The post type (e.g. 'product', 'product_variation').

Return values
array<string|int, string>