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!
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.
}
Thank you for providing these code snippets. Where are the results of these output once added to functions.php?
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