There’s a number of things to learn from this post; namely:
How to get the default variation ID of a variable product.
How to get a variation ID by an array of attributes.
How to get a variation by an array of attributes
In order to fetch the default variation of a variable product in WooCommerce, we first need to know how to find a variation of a product based on an array of known attributes.
Let’s say for example, we were selling t-shirts. We have a variable product for one of our t-shirt designs, and it comes in a few sizes and colours. Now we want to look up the variation ID for the “Small” and “Red” t-shirt. In order to do this, we need access to the main variable $product object, and an array of our attributes. I’ll assume the attributes are set up under Products > Attributes for now.
Let’s prepare an array of the attributes that we want to match to a variation:
Our $attributes array is formed of a key (pa_size and pa_color), which is the attribute taxonomy, and a value (small and red), which is the term slug.
Now that we have our $attributes array, and I’ll assume we already have access to the main $product variable, we need a function that will find the matching variation. WooCommerce actually has one built in. I put a wrapper function around this for 2 reasons; their method requires the attribute keys to be prefixed with attribute_ (which becomes an issue later), and this version will work for WooCommerce 3.* above and below.
First of all, we pass the $attributes array through a loop that adds the attribute_ prefix, if required. Secondly, we check if the WC_Data_Store class exists; if it does, we’re on WooCommerce 3.0.0 or above. This is important because the method to find the matching variation is accessed differently from 3.0.0 and above.
We then pass our $attributes array into the appropriate method, and either a variation ID is returned, or 0.
And that will return the ID of the variation we’re looking for, or 0 if it couldn’t find a match. Awesome, we’re ready to find the default variation ID of our variable product.
How to get the default variation ID of a variable product
We now have a function that helps us get a variation based on a list of attributes. We can use this to get the default variation of our variable product.
One thing to note; our variable product would only have a default variation if you have actually set up some default attribute values for the product, and that they then match one of your variations. For example, this setup would match variation 709:
As our previous function is backwards compatible, let’s look at a function to get the default attributes of your variable product in a backwards compatible way:
No matter the version of WooCommerce, the method always belongs to the $product object. As such, we can just check if the method exists, and if it does, use it.
This function is going to return an array. If the default attributes are set, we’ll get an array of attributes, much like the one we set earlier. If not, we’ll get an empty array. Either way, we can pass this result into our iconic_find_matching_product_variation() function.