WooCommerce: move “custom product attribute” to “taxonomy”

In WooCommerce you can add attributes to a product in two ways.

method #1

a. Create an attribute in Product->Attributes, set a name for it (i.e color), define it wether to be a select-field or a text-field. Save.

Read More

b. Edit product switch to attributes-tab, select “color” from the dropdown, fill in some values. Save.

method #2

Edit product, switch to attributes-tab, select “add custom attribute”, set a name, fill in some values. Save.

Well, in method #1, the attributes are saved and connected over three tables. wp_terms, wp_term_taxonomies and wp_term_relationship

In method #2, the attributes are serialized and saved in wp_postmeta with ‘_product_attributes’ as meta_key.

my problem

What I would like to do now is to move all attributes that are stored in the wp_postmeta table, to the wp_meta table and connect them correctly via wp_term_taxonomies and wp_term_relationship.

Is there any solution or plugin for that?

Moving them manually is not an option since there are over 500 of them.

Cheers,

Related posts

1 comment

  1. There is a WooCommerce-REST-API that allows me to do that.
    http://woothemes.github.io/woocommerce-rest-api-docs/

    First I had to get the slugs and names of all attributes (not their values) that are stored in the wp_term_relathionship. Since there are just a few, I wrote them down.

    1. Get a JSON list of all products
    2. Iterate through the attributes of the product
    3. Compare it’s name and slug to the one written down
    4. If slug doesn’t match, write the values to an attribute with the correct slug.

    Example:
    Product has two attributes called “Color”. The one attribute is stored in wp_term_relationships and has name “Color” and slug “pa_attr_color”. The other color is stored in wp_postmeta as pa_attributes, and has name “Color” and slug “wrong_color”. Now I want to move that values to the pa_attr_color.

    $product = [product => 
      [
      id => 123, 
      attributes => [
        [name => 'Color', 'slug' => 'pa_attr_color', options => ['red', 'blue']],
        [name => 'color', 'slug' => 'wrong_color', options => ['yellow', 'green']]
      ]]
    

    All I have to do now is to move the options from wrong_color to pa_attr_color.
    If there’s no pa_attr_color, than I simply had to create one.

Comments are closed.