Retrieving WooCommerce Product Variations via PHP
When building a custom theme or plugin for WooCommerce, you often encounter the need to fetch product data outside the standard shortcode context. A common scenario involves displaying a list of available variationsâsuch as size or color optionsâfor a specific product on a custom page. The core challenge lies in accessing the variation objects directly using only the parent product ID.
WooCommerce stores variations as child products within the parent's data structure. If you attempt to query the database directly for these items, you risk missing critical metadata that is dynamically managed by the framework. The recommended approach utilizes the WC_Product class and its relationship methods to traverse this hierarchy safely.
To retrieve all variations associated with a specific product ID, you should instantiate a WC_Product object using that ID. Once instantiated, the object provides access to a method designed specifically for this purpose: get_child_ids(). This method returns an array of IDs for every variation linked to the parent product. However, returning just the IDs is often insufficient if your custom page needs to display attributes, prices, or images associated with each variation.
To get full variation objects, you must combine the child ID retrieval with a direct fetch from the WC_Product_Factory. Here is a practical implementation example:
$product_id = 123; // Replace with your dynamic product ID
// Instantiate the parent product
$product = wc_get_product($product_id);
if ($product && $product->get_type() === 'variable') {
// Get the array of variation IDs
$variation_ids = $product->get_child_ids();
if (!empty($variation_ids)) {
foreach ($variation_ids as $var_id) {
// Fetch the full variation object
$variation = wc_get_product($var_id);
// Now you can access attributes, prices, etc.
echo "Variation: " . $variation->get_name() . "<br>";
echo "Price: $" . $variation->get_price() . "<br>";
}
}
}
This pattern ensures that you are working with fully hydrated WC_Product objects rather than raw database records. The get_child_ids() method is the key here because it leverages the internal relationship table WooCommerce maintains between parent and child products. Attempting to bypass this by querying the wp_posts table directly without using the factory methods will likely yield incomplete data or fail to respect product types (e.g., distinguishing between a simple variation and an external affiliate link).
One critical pitfall to consider is handling variable products that have no variations defined yet. In such cases, get_child_ids() returns an empty array, but checking for the existence of the product itself isn't enough; you must verify the product type as well. A simple variable product without any configured variations will still pass the initial existence check but yield zero results from the variation fetcher.
Additionally, be mindful of performance when iterating through many products. If your custom page displays multiple products dynamically, ensure you are not unnecessarily re-initializing the WC_Product object for every single item if you can cache the data or use batch processing methods where applicable. The factory functions like wc_get_product() are generally optimized, but in high-traffic environments, caching strategies become essential to prevent repeated database queries.
If you find yourself needing more complex logic around variation attributes or custom fields, remember that these objects extend standard WordPress post structures. This means you can still use global functions like get_the_title or get_post_meta, but relying on the WooCommerce-specific methods ensures consistency with the rest of the platform's data model.