In a How WooCommerce Stores Product Variations Part 1, I discussed how variations are kept in the database as the relationship between the posts and postmeta table.
However, the article did not address using global attributes with product variations.
What are Global Attributes?
Global attributes are the attributes that you can set in your dashboard under products. With these attributes, you set a name for your attribute and then configure the terms associated with it
For example, an attribute named color could have terms such as red, green and blue. Then, the attributes and selected terms can be added to the product.
After clicking the add button, clicking Select All will place all of the terms as part of the attributes. Unchecking a box removes that term for that product only. Make sure the “Used for Variations” checkbox is selected before clicking the save attributes button.
Once the global attributes are added to your product, they can be added as variations.
Where are Global Attributes Stored in the Database?
Let’s say our parent id is 26 and it has a variation id of 44. As discussed in the previous article, both the product and its variations are stored in the posts and postmeta tables.
In this case, the terms in the global attributes that were set up must also be handled. WooCommerce makes use of three (3) additional tables for this.
First, if we look in the Term Relationships table, we can find our product id of 26 and see all of the term taxonomy IDs.
The term taxonomy IDs lead us to the Term Taxonomy table, where we can see what the taxonomy of any those IDs represent. So, if we see pa_color, we know that is the global color attribute we added.
Moving on, we can examine the term ID field which naturally leads us to the Terms table. In the terms table, we can see that the ID represents a specific term or color such as blue.