Automattic\WooCommerce\Blocks\SharedStores
ProductsStore::load_variations
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() ProductsStore::load variations code WC 10.6.2
public static function load_variations( string $consent_statement, int $parent_id ): array {
self::check_consent( $consent_statement );
$response = Package::container()->get( Hydration::class )->get_rest_api_response_data( '/wc/store/v1/products?parent[]=' . $parent_id . '&type=variation' );
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_state();
return $keyed_variations;
}