WooCommerce get_sku(); on products variations in order
When handling custom logic for WooCommerce orders, a common friction point arises when trying to distinguish between a standard product and its specific variations. You need to retrieve the base SKU if the item is a simple product, but hit a snag when dealing with variable products where $product->get_sku() returns an empty string or the parent's generic SKU instead of the variation-specific identifier stored in the order data.
The core issue lies in how WooCommerce structures its objects. When you instantiate a WC_Product using $order->get_items(), you are typically interacting with the parent product object, not the specific variation instance that was purchased. The order item itself holds the necessary data in the $item['variation_id'] key, but simply checking if this ID exists isn't enough; you must actively construct the variation object to access its properties.
Inspecting Order Items Correctly
To resolve this, you need to look closely at the $item array returned by get_items(). The most reliable way to determine if an item is a variation is to check for the existence of the variation_id key. If it exists and is greater than zero, you are dealing with a variable product line item.
Here is a robust pattern to handle both scenarios within your loop:
foreach ($order->get_items() as $item) {
$product_id = $item['product_id'];
$qty = $item['qty'];
$variation_id = $item['variation_id'] ?? null;
// Always fetch the product data object first
$product = new WC_Product($product_id);
$sku = '';
if ( ! empty( $variation_id ) ) {
// If a variation ID exists, we must instantiate the specific variation
// to get the variation-specific SKU.
$variation = new WC_Product_Variation( $variation_id );
$product = $variation; // Update reference to use variation data
// Now get_sku() will return the variation's unique SKU
$sku = $product->get_sku();
} else {
// For simple products or virtual/variable parents without specific variation
$sku = $product->get_sku();
}
// Log or process the result
if ( ! empty( $sku ) ) {
echo "SKU: {$sku} - Qty: {$qty}\n";
}
}
Why get_available_variations() Is Not The Solution
The source material mentions an attempt to use $product->get_available_variations(). While this method is useful for frontend logicâlike rendering dropdowns in the cart or shortcode loopsâit introduces significant overhead and complexity when processing order data.
get_available_variations() retrieves all possible variations defined for a product, regardless of whether they were actually purchased. More critically, it relies on global settings like visibility_in_catalog, manage_stock, and price filters. If you are building a function that needs to strictly reflect what was in the cart, querying available inventory logic is unnecessary noise. It forces WooCommerce to re-evaluate stock statuses and visibility rules that have nothing to do with the transaction history.
The direct approach of checking $item['variation_id'] and instantiating WC_Product_Variation is significantly more performant. It bypasses the need to query the database for a list of children, instead fetching the specific row ID that corresponds to the exact item purchased. This reduces memory usage and API calls, which is vital when processing large datasets or high-volume order reports.
Handling Edge Cases
Be aware of a few edge cases when implementing this logic. First, ensure you are using WC_Product_Variation rather than passing the ID back into new WC_Product(). While WooCommerce classes often extend one another, explicitly calling WC_Product_Variation ensures you are accessing methods specific to that subclass, such as unique variation properties if they exist beyond standard product fields.
Secondly, always validate that $variation_id is actually an integer before passing it to the constructor. In some rare plugin conflicts or legacy data migrations, a variation ID might be stored as a string or null unexpectedly. The ternary operator ?? null in the example above helps prevent silent failures where a falsey value might be interpreted incorrectly.
Finally, remember that $product->get_sku() on a simple product will return the parent SKU if no variation is present. This behavior is consistent and expected. If you are building a reporting system where empty SKUs indicate missing data, you might want to add a check: if ( $sku === '' && $variation_id ) { /* handle error */ }.
By sticking to the object instantiation pattern based on the presence of a variation ID, you ensure your code remains clean, efficient, and independent of global product visibility settings.