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:
- Obtain an array of ID's for all of the products in the database
- 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:
Custom attributes:
Thanks