Automattic\WooCommerce\Blocks\SharedStores
ProductsStore::register_getters
Register the derived-state getters once.
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.
Method of the class: ProductsStore{}
No Hooks.
Returns
null. Nothing (null).
Usage
$result = ProductsStore::register_getters(): void;
ProductsStore::register_getters() ProductsStore::register getters code WC 10.9.4
private static function register_getters(): void {
if ( self::$getters_registered ) {
return;
}
self::$getters_registered = true;
wp_interactivity_state(
self::$store_namespace,
array(
'mainProductInContext' => function () {
$context = wp_interactivity_get_context();
$state = wp_interactivity_state( self::$store_namespace );
$product_id = array_key_exists( 'productId', $context )
? $context['productId']
: ( $state['productId'] ?? null );
if ( ! $product_id ) {
return null;
}
return $state['products'][ $product_id ] ?? null;
},
'productVariationInContext' => function () {
$context = wp_interactivity_get_context();
$state = wp_interactivity_state( self::$store_namespace );
$variation_id = array_key_exists( 'variationId', $context )
? $context['variationId']
: ( $state['variationId'] ?? null );
if ( ! $variation_id ) {
return null;
}
return $state['productVariations'][ $variation_id ] ?? null;
},
'productInContext' => function () {
$state = wp_interactivity_state( self::$store_namespace );
$selected = $state['productVariationInContext'] instanceof \Closure
? $state['productVariationInContext']()
: $state['productVariationInContext'];
if ( $selected ) {
return $selected;
}
return $state['mainProductInContext'] instanceof \Closure
? $state['mainProductInContext']()
: $state['mainProductInContext'];
},
)
);
}