If you’re working with variable products in WooCommerce, there may come a time when you need to fetch product variations programmatically—whether for display, customization, or integration purposes.
In today’s guide, I’ll show you three proven ways to retrieve product variations using WooCommerce’s powerful API and object-oriented tools. Two methods utilize the WC_Product
object directly, and the third taps into the WooCommerce REST API, which is particularly useful for external applications or syncing between stores.
Getting Started
Before we begin, let’s assume you already have access to a $product
object—typically retrieved via a hook or by using:
$product = wc_get_product( $product_id );
Make sure to confirm that you’re working with a variable product using:
if ( $product->is_type( 'variable' ) ) {
// safe to proceed
}
Method 1: Using get_available_variations()
This method returns an array of variation data for enabled and in-stock product variations.
$variations = $product->get_available_variations();
if ( $variations ) {
foreach ( $variations as $variation ) {
echo $variation['variation_id']; // Variation ID
echo $variation['attributes']['attribute_pa_size']; // Example attribute
}
}
Note:
This method only returns visible and in-stock variations by default.
How to Show Hidden or Out-of-Stock Variations
To override the default behavior and include hidden/out-of-stock variations:
- Go to: WooCommerce → Settings → Products → Inventory
- Uncheck “Hide out of stock items from the catalog”
Or programmatically:
add_filter( 'woocommerce_hide_invisible_variations', '__return_false' );
You can also customize with a function:
add_filter( 'woocommerce_hide_invisible_variations', 'show_all_variations', 10, 3 );
function show_all_variations( $hide, $variation_id, $variation ) {
return false;
}
Method 2: Using get_children()
+ wc_get_product()
If you want all variations, regardless of their visibility or stock status, use this method:
$variation_ids = $product->get_children();
if ( $variation_ids ) {
foreach ( $variation_ids as $variation_id ) {
$variation = wc_get_product( $variation_id );
if ( ! $variation || ! $variation->exists() ) {
continue;
}
echo $variation->get_id();
echo $variation->get_name();
}
}
This is the most complete and flexible method for retrieving variations.
Method 3: Using the WooCommerce REST API
Need to fetch variations from another WooCommerce site or integrate with an external app? Use the WooCommerce REST API.
Example with wp_remote_get()
and Basic Auth:
$url = 'https://example.com';
$username = 'your-username';
$password = 'your-application-password'; // Use Woo App Passwords
$response = wp_remote_get(
add_query_arg(
array(
'per_page' => 100,
// 'status' => 'publish',
// 'stock_status' => 'instock',
),
"{$url}/wp-json/wc/v3/products/{$product_id}/variations"
),
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$username}:{$password}" )
)
)
);
$variations = json_decode( wp_remote_retrieve_body( $response ), true );
if ( $variations ) {
foreach ( $variations as $variation ) {
echo $variation['id'];
echo $variation['sku'];
}
}
Important Notes:
- Make sure to handle pagination with
per_page
andpage
parameters. - You can also use Consumer Key/Secret for API authentication.
- This method is ideal for headless WooCommerce setups or cross-site product syncing.
Final Thoughts
Whether you’re building a custom product table, managing stock programmatically, or syncing stores, these three methods will help you access WooCommerce product variations easily.
- Use
get_available_variations()
for frontend-ready, visible variations. - Use
get_children()
for complete control over all variations. - Use the REST API for external or remote applications.