Get Product Variations Programmatically
Soon, you will learn three ways how you can get product variations programmatically in your code. In the first and in the second way we are simply going to use the WC_Product object (or, to be more specific, the WC_Product_Variation object). The third way is going to be a little bit more interesting – I will show how to do it via the WooCommerce REST API.
Frankly, I remember, I wanted to publish this mini-guide a long time ago, I believe I didn’t because I found an amazing explanation on another website. But guess what – today that website doesn’t exist anymore. So, here we go.
Let’s assume, that in the examples below, $product is a WC_Product product object, or, to be more specific, WC_Product_Variable, which we can get as an argument when working with a WooCommerce hook or with the help of the wc_get_product() function.
But if you’re not sure that a $product object belongs to a variable product, you can also use a condition if( $product->is_type( 'variable' ) ) .
Method 1. get_available_variations()
$variations = $product->get_available_variations();
if( $variations ) {
foreach( $variations as $variation ) {
// a WC_Product_Variation object
echo $variation->get_id(); // ID of this specific variation
echo $variation->get_name();
}
}This method may seem like exactly what we need, but there is a small “but” – it works only for visible in-stock variations, I mean for the variations with a checked “Enabled” checkbox:

get_available_variations() method.Nothing to worry about – if you need to get all variations, you can either use a get_children() method that we will discuss below, or continue reading the next chapter.
How to get all variations using get_available_variations()
As I mentioned before, the get_available_variations() will only return the variations that are in stock and not hidden. Is there a way to bypass that?
Yes.
First of all, you can allow this method to return out-of-stock variations if you go to the WooCommerce settings and uncheck this checkbox:

Second, you can use the filter hook woocommerce_hide_invisible_variations in order to make the get_available_variations() method to return hidden (not enabled) variations as well:
add_filter( 'woocommerce_hide_invisible_variations', 'rudr_all_variations', 25, 3 );
function rudr_all_variations( $hide_invisible, $variation_id, $variation ) {
// you can also add some variation-specific conditions here
$hide_invisible = false;
return $hide_invisible;
}Or a simplified version:
add_filter( 'woocommerce_hide_invisible_variations', '__return_false' );Method 2. get_children()
The get_available_variations() method we discussed above works exactly on the basis of the get_children() method.
But, if you need to get both hidden and visible, in-stock and out-of-stock variations of a WooCommerce product, the get_children() method will probably be better for you. You only need to keep in mind one thing – it returns an array of variation IDs, not objects.
$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();
}
}Method 3. Get Product Variations in WooCommerce REST API
Also, I’d like to discuss how to get product variations programmatically from another WooCommerce store when you’re working with the REST API.
I wanted to show you this method as well because I’m working with the WooCommerce REST API a lot, especially in my Simple WP Crossposting plugin which allows you to sync products and variations between stores.
// credentials
$url = '';
$username = '';
$pwd = ''; // an application password
// sending a request to get product variations in WooCommerce
$response = wp_remote_get(
add_query_arg(
array(
'per_page' => 100, // default is 10
// 'status' => 'publish', // get only enabled (visible) variations
// 'stock_status' => 'instock', // get only in-stock variations
),
"{$url}/wp-json/wc/v3/products/{$product_id}/variations"
),
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "{$username}:{$pwd}" )
)
)
);
// good to check a response code here
$variations = json_decode( wp_remote_retrieve_body( $response ), true );
if( $variations ) {
foreach( $variations as $variation ) {
echo $variation[ 'id' ];
echo $variation[ 'sku' ];
}
}Let’s talk about this code a little bit:
- As you can see, I am using a WordPress HTTP API function –
wp_remote_get(), but you can feel free to use official JS and PHP libraries for WooCommerce REST API if you want. - To authenticate you need to provide either a username and an application password or a consumer key and a consumer secret.
- Pay attention to the
per_pageparameter, because its default value is10, which means, that if your products have a lot of variations, you can not get all of them in a single REST API request.
Guys, if you have any questions, please let me know in the comments below.
Misha Rudrastyh
Hey guys and welcome to my website. For more than 15 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me