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 (for example):
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.
Thanks.
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:
wp_woocommerce_attribute_taxonomies
.attribute_name attribute_label attribute_id attribute_type attribute_orderby attribute_public
color color 1 select menu_order 0
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
wp_term_taxonomy
.'color'
.term_id
you will have a slug string beginning with 'pa_
(that mean product attribute) + the main attribute slug color
.'pa_color'
term_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
wp_term_relationships
you will find the relationships between products (object_id
column, used by variations for example) and sub-attributes (term_taxonomy_id
column). 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_taxonomy
DB table.
- Search all sub-attributes
term_id
inwp_terms
DB table.- Compare them to existing
term_id
inwp_term_taxonomy
DB table- Create for all non existing
term_id
inwp_term_taxonomy
with 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
: