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
 * @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
 * @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
 * @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

  • WooCommerce: Custom Add to Cart URLs – The Ultimate Guide
    In WooCommerce you can add a product to the cart via a custom link. You just need to use the “add-to-cart” URL parameter followed by the product ID. This tutorial will show you how to create custom URLs to add simple, variable and grouped products to the cart – as well as defining the add […]
  • WooCommerce: Add Custom Field to Product Variations
    Adding and displaying custom fields on WooCommerce products is quite simple. For example, you can add a “RRP/MSRP” field to a product, or maybe use ACF and display its value on the single product page. Easy, yes. Unfortunately, the above only applies to “simple” products without variations (or the parent product if it’s a variable […]
  • WooCommerce: Display “FREE” Instead of $0.00 Price
    In older versions of WooCommerce free prices used to display as “FREE!” and products with empty prices were not publishable/purchasable. Now they’ve changed this around, but I still believe “FREE” looks much better than “$0.00”. It’s much more enticing, isn’t it? Well, here’s how you restore the old WooCommerce functionality – as usual it’s as […]
  • WooCommerce: Display Variations’ Stock @ Shop Page
    Thanks to the various requests I get from Business Bloomer fans, this week I’m going to show you a simple PHP snippet to echo the variations’ name and stock quantity on the shop, categories and loop pages. Of course, if “Manage stock” is not enabled at variation level, the quantity will be null, and therefore […]
  • WooCommerce: How to Display Variations with Color / Size Buttons?
    Displaying product pages nicely is the entrepreneur’s dream. Good UX means a much higher probability the interested customer is going to add to cart and complete the checkout. However, WooCommerce variable products come with annoying dropdowns for each attribute (color, size, style, etc. depending on what options you have set up). And as you’ve already […]

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

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 *