WooCommerce Get Product Variations Programmatically [Guide]

Get WooCommerce product variations programmatically

Many stores need product data outside the product page: a feed, a custom checkout summary, an API call, or a report listing every size and color. The data you need is in the variations, because WooCommerce stores each variation as a child of one variable product.

That’s what getting product variations in code is for. This guide covers three methods, get_available_variations(), get_children(), and the REST API, and explains when each one fits. To list parent products first, see the wc_get_products() function guide.

Method 1: get_available_variations()

get_available_variations() is a method of the variable product class. It returns one array per variation with the attributes, price, image, and stock status already formatted. WooCommerce uses the same data to build the variation dropdowns on the product page, so it is the fastest choice when you need display-ready data.

Loop Through the Variation Data

Load the product with wc_get_product(), confirm it is variable, then loop through the results. Each entry includes keys such as variation_id, attributes, and is_in_stock.

<?php
$product = wc_get_product( 123 );

if ( ! $product || ! $product->is_type( 'variable' ) ) {
    return;
}

$variations = $product->get_available_variations();

foreach ( $variations as $variation ) {
    $variation_id = $variation['variation_id'];
    $attributes   = $variation['attributes'];
    $in_stock     = $variation['is_in_stock'];
}

[screenshot] Product edit screen, Variations tab, with two or three variations expanded so the attribute values are visible.

Tip: Call get_available_variations() once and store the result in a variable. Each call rebuilds the data, so repeated calls in the same request waste database queries.

Method 2: get_children()

get_children() returns the IDs of the product’s variation posts, ordered by menu order. It returns IDs only, so you load each variation yourself. Choose it when you need the full variation objects, for example to read the SKU or the regular price of each one.

Load Full Variation Objects

Loop through the IDs, load each variation with wc_get_product(), and read the getter you need. This method does not filter by stock level, so call is_in_stock() on each variation if you only want sellable ones.

<?php
$product = wc_get_product( 123 );

if ( ! $product || ! $product->is_type( 'variable' ) ) {
    return;
}

$skus = array();

foreach ( $product->get_children() as $variation_id ) {
    $variation = wc_get_product( $variation_id );

    if ( $variation ) {
        $skus[] = $variation->get_sku();
    }
}

Method 3: Get Variations With the REST API

The WooCommerce REST API exposes variations at /wc/v3/products/{id}/variations. Use it when another system or a different site needs the data. The endpoint requires API credentials, so keep the keys out of theme files and never call it from front-end JavaScript. For code running on the same site, Methods 1 and 2 are faster because they skip the HTTP request.

<?php
$consumer_key    = 'ck_your_consumer_key';
$consumer_secret = 'cs_your_consumer_secret';

$response = wp_remote_get(
    'https://example.com/wp-json/wc/v3/products/123/variations?per_page=100',
    array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( $consumer_key . ':' . $consumer_secret ),
        ),
    )
);

if ( is_wp_error( $response ) ) {
    return;
}

$variations = json_decode( wp_remote_retrieve_body( $response ), true );

Which Method to Use

Pick the method by what you do with the result.

MethodReturnsBest for
get_available_variations()Display-ready arrays with attributes, price, image, and stockProduct pages and custom dropdowns
get_children()Variation IDsLoading full objects for custom logic
REST APIJSON over HTTPOther systems and remote sites

FAQs About WooCommerce Product Variations

How Do I Get Variation Prices?

Call get_price() on a variation object from Method 2. It returns the active price, including a sale price when one is set. With Method 1, read the display_price key instead.

Does It Work for Simple Products?

No. Simple products have no variations, so both methods return nothing useful. Check is_type( 'variable' ) first, as the examples do.

Is It Safe to Call This in a Loop?

Avoid calling it for every product on a shop or archive page, because each call queries the database. When you need variation data across many products, store the result in a transient for a set time.

Can I Get Only In-Stock Variations?

Yes. Filter the Method 1 results by the is_in_stock key:

<?php
$in_stock = array_filter(
    $product->get_available_variations(),
    function ( $variation ) {
        return $variation['is_in_stock'];
    }
);

Conclusion

For theme and plugin code on your own site, get_available_variations() covers most needs in a few lines. Reach for get_children() when you need full objects, and use the REST API only when the data has to leave the site.

Need a product feed or a custom variation workflow built for your store? Hire a WooCommerce developer, or reach out to me directly.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.