WooCommerce自动添加Parent属性

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):

  • 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.

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:

  1. 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
  1. 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
  1. 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-attribute term_id you will have a slug string beginning with 'pa_ (that mean product attribute) + the main attribute slug color.
    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-category 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
  1. In DB table 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 is wp_term_taxonomy DB table.

  1. Search all sub-attributes term_id in wp_terms DB table.
  2. Compare them to existing term_id in wp_term_taxonomy DB table
  3. Create for all non existing term_id in wp_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 :