Convert WooCommerce Custom Product Attributes into Defined Product Attributes
Asked Answered
P

3

7

I am trying to filter ~30,000 products in a WooCommerce store using WooCommerce's Layered Nav attribute filter sidebar widget. This requires the use of a pre-defined product attribute taxonomy, as opposed to using custom attributes on a per-product basis.

Each product in the store has an imported custom 'Brand' attribute. Prior to importing products, I created an attribute taxonomy called 'Brand' (pa_brand) in wp-admin. However, imported products have not had their Brand attribute (and term name) added to the Brand (pa_brand) taxonomy. Instead, all of the products have a custom attribute called 'Brand' not linked to the pa_brand taxonomy.

Does anyone know how to convert custom product attributes into defined attributes within the pre-defined taxonomy (with terms not yet created)?

I assume that I'd need to run a PHP script which does the following, but don't know if I've missed any steps out:

  1. Obtain an array of ID's for all of the products in the database
  2. Loop over each of the product ID's (foreach)
    • get_attributes() from each product ID - this will return the custom 'Brand' attribute (there are no pa_brand assignments yet)
    • Check: If the brand term already exists in the pa_brand taxonomy, do nothing; if it doesn't, add the term to the pa_brand taxonomy
    • Assign the product to the term within the pa_brand taxonomy
    • Delete the old custom Brand product attribute

I'm not sure how best to execute this, so any help would be very much appreciated.

PS: to illustrate the distinction between 'custom attributes' and attributes defined in a taxonomy,
I have added 2 screenshots below of how they differ when editing a product in WooCommerce. Note how the name of the attribute can be altered if not from the defined taxonomy.

Attributes defined in taxonomy:

Attributes defined in taxonomy

Custom attributes:

Custom attributes

Thanks

Piquet answered 14/8, 2016 at 15:32 Comment(0)
P
4

It looks like someone has done this before - https://gist.github.com/birgire/0ed300ae4436fcaf508c

Piquet answered 20/8, 2016 at 10:18 Comment(0)
C
2

just to help anyone that comes to this page sharing the same problem. Custom attribute to global (taxonomy), variations, etc...

How we solved it easily:

  1. We created the same name of the Custom Attribute as a Global Attribute (Brand => Brand). No need to add terms, just attribute Name.

  2. Downloaded (Export) all the products as CSV file on the All Products table page.

  3. On the CSV file (opened on Excel) make sure to set Attribute n global (n is the number of attribute, 1, 2, etc...) to 1 (when it's custom it will show 0). Do this on all rows that have the attribute you are converting.

  4. To make sure Variations also saved and that attribute can be used make sure that Attribute n visible is also set to 1 on all rows.

  5. Save the file.

  6. Next go back to your All Products page and click on Import.

  7. Choose the file and check the box that says "Existing products that match by ID or SKU will be updated. Products that do not exist will be skipped.".

  8. Next click on Run the importer button.

This will convert all the custom attributes with their terms into global, and also keep all the variations. No need for any coding or hard work.

For the steps 3 and 4 you can use formula like this one =IF(AN2="Flavors","1","") for the first line and copy paste it to all rows to do it automatically for a specific attribute if needed.

Hopefully this will help you.

Cheshire answered 9/11, 2022 at 21:3 Comment(0)
E
0

Just to add/clarify on @user3084448's solution (and thank you for this, it has helped immensely)-

When you execute step 2, you may be tempted to export only a few fields, such as ID, SKU and Attributes. If you proceed from here your import shall succeed but you will loose all of your variations per product!

For step 2 to work either export all fields, or if you find this isn't working (e.g. the import is too slow and won't complete), minimally export ID, SKU, Parent and Attributes fields.

It seems for this to work the importer requires the Parent field to be included.

Explore answered 22/1, 2024 at 23:26 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.