Automattic\WooCommerce\Blocks\SharedStores

ProductsStore::load_variationspublic staticWC 1.0

Load all variations of a variable product into state.

Method of the class: ProductsStore{}

No Hooks.

Returns

Array. The variations keyed by ID.

Usage

$result = ProductsStore::load_variations( $consent_statement, $parent_id ): array;
$consent_statement(string) (required)
The consent statement string.
$parent_id(int) (required)
The parent product ID.

ProductsStore::load_variations() code WC 10.9.1

public static function load_variations( string $consent_statement, int $parent_id ): array {
	self::check_consent( $consent_statement );

	// Skip loading if variations for this parent have already been loaded.
	if ( isset( self::$loaded_variation_parents[ $parent_id ] ) ) {
		return array_filter(
			self::$product_variations,
			fn( $variation ) => ( $variation['parent'] ?? 0 ) === $parent_id
		);
	}

	$response = Package::container()->get( Hydration::class )->get_rest_api_response_data( '/wc/store/v1/products?parent[]=' . $parent_id . '&type=variation' );

	self::$loaded_variation_parents[ $parent_id ] = true;

	if ( empty( $response['body'] ) ) {
		return array();
	}

	// Re-key array by variation ID and merge into state.
	// Use array_replace instead of array_merge to preserve numeric keys.
	$keyed_variations         = array_column( $response['body'], null, 'id' );
	self::$product_variations = array_replace( self::$product_variations, $keyed_variations );
	self::register_getters();
	wp_interactivity_state(
		self::$store_namespace,
		array( 'productVariations' => $keyed_variations )
	);

	return $keyed_variations;
}