'WooCommerce Adding Parent attribute automatically
I have a WooCommerce site with auto-imported products, which refresh them every week. These products have attributes called COLOR and SIZE.
However, the COLOR red has these sub-attributes:
- Crimson Red
- Cola Red
- Puma Red
Normally, I have to manually add and assign these sub-attributes to the "parent RED" attribute and that is a lot of work.
I wonder if there is a way to do this automatically?
For example, IF the attribute contains the word RED, assign it to parent attribute RED.
Solution 1:[1]
This could be see as a strange answer. First it should help you to understand how things are going in WordPress/WooCommerce database and may be this way it could be a good alternative if you understand something about Database SQL or MySQL.
Here is explained first how WooCommerce attributes and sub-attributes are set in database:
- When you create an attribute, it goes on DB table
wp_woocommerce_attribute_taxonomies.
attribute_name attribute_label attribute_id attribute_type attribute_orderby attribute_public
color color 1 select menu_order 0
- Each sub attribute goes in DB table
wp_terms(so they are terms). Here I have 3 sub-attributes:'black','blue'and'green':
term_id name slug term_group
8 Black black 0
9 Blue blue 0
10 Green green 0
- The relationships between this main attribute and his sub-attributes is located in DB table
wp_term_taxonomy.
Now imagine the slug of your master attribute is'color'.
Then for each sub-attributeterm_idyou will have a slug string beginning with'pa_(that mean product attribute) + the main attribute slugcolor.
So you will get finally:'pa_color'
(hope this is clear).
Now in this table you have a 'count' column that counts the number of products related to this sub-categoryterm_id.
term_taxonomy_id term_id taxonomy description parent count
8 8 pa_color 0 2
9 9 pa_color 0 1
10 10 pa_color 0 1
- In DB table
wp_term_relationshipsyou will find the relationships between products (object_idcolumn, used by variations for example) and sub-attributes (term_taxonomy_idcolumn). As you can see on the example below, the sub-attributes are used by 2 products (object_id):
object_id term_taxonomy_id term_order
22 8 0
40 8 0
40 9 0
22 10 0
A SQL solution:
For you if you can alter database tables, the useful table to alter iswp_term_taxonomyDB table.
- Search all sub-attributes
term_idinwp_termsDB table.- Compare them to existing
term_idinwp_term_taxonomyDB table- Create for all non existing
term_idinwp_term_taxonomywith the correct'taxonomy'slug for each one.This is possible with SQL queries through PHP (that you will need to fine tune to feet your very specific needs).
I hope this will help you a bit.
Some useful threads, related to SQL wp_term_taxonomy :
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
