WooCommerce Code Reference

ProductsStore
in package

Shared store that hydrates the `woocommerce/products` Interactivity API store with product and variation data in Store API format.

The store exposes two planes:

  • Raw data (products, productVariations) populated by the load_* methods below, each keyed by ID.
  • Selection (productId, variationId) — set by callers via wp_interactivity_state (global) or data-wp-context (per-element) — plus the derived getters (mainProductInContext, productVariationInContext, productInContext) registered by register_getters().

The derived getters are mirrored in the JS store (client/blocks/assets/js/base/stores/woocommerce/products.ts) so that directive bindings like state.productInContext.sku resolve during server-side rendering as well as on the client.

See client/blocks/assets/js/base/stores/woocommerce/README.md for the full model and consumer examples.

This is an experimental API and may change in future versions.

Table of Contents

$consent_statement  : string
The consent statement for using this experimental API.
$getters_registered  : bool
Whether the derived-state getters have been registered.
$loaded_variation_parents  : array<string|int, mixed>
Parent product IDs whose variations have already been loaded.
$product_variations  : array<string|int, mixed>
Product variations that have been loaded into state.
$products  : array<string|int, mixed>
Products that have been loaded into state.
$store_namespace  : string
The namespace for the store.
load_product()  : array<string|int, mixed>
Load a product into state.
load_purchasable_child_products()  : array<string|int, mixed>
Load all purchasable child products of a parent product into state.
load_variations()  : array<string|int, mixed>
Load all variations of a variable product into state.
check_consent()  : true
Check that the consent statement was passed.
register_getters()  : void
Register the derived-state getters once.

Properties

The consent statement for using this experimental API.

private static string $consent_statement = 'I acknowledge that using experimental APIs means my theme or plugin will inevitably break in the next version of WooCommerce'

$loaded_variation_parents

Parent product IDs whose variations have already been loaded.

private static array<string|int, mixed> $loaded_variation_parents = array()

Methods

load_product()

Load a product into state.

public static load_product(string $consent_statement, int $product_id) : array<string|int, mixed>
Parameters
$consent_statement : string

The consent statement string.

$product_id : int

The product ID.

Tags
throws
InvalidArgumentException

If consent statement doesn't match.

Return values
array<string|int, mixed>The product data.

load_purchasable_child_products()

Load all purchasable child products of a parent product into state.

public static load_purchasable_child_products(string $consent_statement, int $parent_id) : array<string|int, mixed>
Parameters
$consent_statement : string

The consent statement string.

$parent_id : int

The parent product ID.

Tags
throws
InvalidArgumentException

If consent statement doesn't match.

Return values
array<string|int, mixed>The purchasable child products keyed by ID.

load_variations()

Load all variations of a variable product into state.

public static load_variations(string $consent_statement, int $parent_id) : array<string|int, mixed>
Parameters
$consent_statement : string

The consent statement string.

$parent_id : int

The parent product ID.

Tags
throws
InvalidArgumentException

If consent statement doesn't match.

Return values
array<string|int, mixed>The variations keyed by ID.

Check that the consent statement was passed.

private static check_consent(string $consent_statement) : true
Parameters
$consent_statement : string

The consent statement string.

Tags
throws
InvalidArgumentException

If the statement does not match.

Return values
true

register_getters()

Register the derived-state getters once.

private static register_getters() : void

These closures mirror the JS getters in client/blocks/assets/js/base/stores/woocommerce/products.ts so that directives referencing state.mainProductInContext / state.productVariationInContext / state.productInContext resolve during SSR. Because they read from wp_interactivity_state() at call time, they only need to be registered once regardless of how many products are added.

Return values
void