WooCommerce: Get All Variations For A Given Product

When you deal with WooCommerce variable products and custom code, you may need to programmatically access all the variations based on a given (parent) product ID.

This post dives into the world of WooCommerce product variations and shows you two powerful PHP methods to retrieve all the variations associated with a specific product ID.

We’ll explore several approaches, and by the end, you’ll be equipped to effortlessly handle variations in your code, based on what exact output you need. Enjoy!

Of course you can “get” the variable product variations by actually looking at the IDs in the product edit page > Variations tab. Or you can be smarter and use any of the three methods below!

PHP Snippet 1: Get Variations Associated With Specific Product (get_available_variations)

The get_available_variations function in WooCommerce is a method specifically designed to retrieve information about the available variations for a variable product.

It takes a WooCommerce product object as input, and then returns an array of product variations, limiting this to only those variations that are currently in stock and purchasable.

Each element in the array represents a single variation object and typically contains details like variation_id, attributes and overall product data (stock, price, images, etc.).

Usage:

/**
 * @snippet       Get Product Variations (get_available_variations)
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

global $product;
if ( $product->is_type( 'variable' ) ) {
   $variations = $product->get_available_variations();
   foreach ( $variations as $variation ) {
      $id = $variation->get_id();
      $sku = $variation->get_sku();
      $stock = wc_get_stock_html( $variation );
      // etc.
   }
}

PHP Snippet 2: Get Variations Associated With Specific Product (get_children)

This function is designed to retrieve child posts of a specific parent post. In WooCommerce, variable products have child posts representing their individual variations. So, get_children can return all the variation IDs for a variable product, without any filtering (a lĂ  get_available_variations).

Moreover, get_children doesn’t offer any data specific to variations. It simply returns IDs, which require further processing to get actual variation details.

Usage:

/**
 * @snippet       Get Product Variations (get_children)
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

global $product;
if ( $product->is_type( 'variable' ) ) {
   $variation_ids = $product->get_children();
   foreach ( $variation_ids as $variation_id ) {
      $variation = wc_get_product( $variation_id );
      $variation_price = $variation->get_price();
      $variation_image = $variation->get_image_id();
      // etc.
   }
}

PHP Snippet 3: Get Variations Associated With Specific Product (wc_get_products)

Imagine your WooCommerce store is like a big box of all your amazing products. The wc_get_products function is like a special tool that helps you reach inside the box and pull out specific things.

You can tell the function exactly what you want to find. Let’s say you only want the red hats. You can tell the tool to look for “red hats” and it will only bring out those products. You can tell the tool to look in the “shirts” section of the box and it will bring out all the different shirt products. You can tell the tool to look for products that are marked as “on sale” and it will only bring out the blue items with special sale prices.

Of course, you can tell the tool to also return the product variations for a product ID! Here are the docs.

Usage:

/**
 * @snippet       Get Product Variations (wc_get_products)
 * @tutorial      Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli, Business Bloomer
 * @compatible    WooCommerce 8
 * @community     Join https://businessbloomer.com/club/
 */

$args = array(
	'parent' => 222239, // variable product ID
	'type' => 'variation',
);
$variations = wc_get_products( $args );
foreach ( $variations as $variation ) {
   $id = $variation->get_id();
   $sku = $variation->get_sku();
   $stock = wc_get_stock_html( $variation );
   // etc.
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

2 thoughts on “WooCommerce: Get All Variations For A Given Product

  1. Thank you for providing these code snippets. Where are the results of these output once added to functions.php?

    1. Good question Andrew! The snippets are only “getting” data and not outputting it. So, you should use these snippets inside a function that is trigered by a hook, and the function at the end should “echo” the data. Hope this makes sense

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *