Import attribute for a product
In this article we'll explain how to automatically import attributes for a WooCommerce product during a Product Set import/update. Plus, lots of code examples at the end of this article!
Examples of common attributes you might add to your products are:
- Age Group
This article is about adding attributes to many products. If you just want to add an attribute to one product (ex. adding an EAN or UPC value to a specific product), you should use the this tutorial.
Please read these requirements before you begin.
1. Before you can import attributes for your products, the attributes you want to import must be added here: WordPress Admin Area > Products > Attributes > Add new attribute. Learn more about WooCommerce Attributes
2. Understand how to use WooCommerce's Product Attribute Filters.
3. Install the custom plugin if you haven't done so already.
OK, let's begin!
Import Attribute from a Specific Field
This is the most basic method of importing a single field as an attribute for a product.
Let's say all of your products have a
color field that you want to import like this:
The code you would add to your custom plugin would look like this:
Now after your Product Sets update, if a product has a
color field, the value of the field will be imported for that product.
Import Attribute from Multiple Fields
Sometimes the attribute value you want to import exists in different fields depending on the merchant.
For example, here are 2 products from 2 different merchants where their "gender" information is stored in different fields:
Now when the Product Set updates, first it will try to import the
gender field's value. If that doesn't exist, it will try to import the
attribute6 field's value. If neither of those fields exist, nothing will be imported.
Import Multiple Attributes from a Single Field
Sometimes a field will contain multiple attribute values separated by a character such as a comma.
For example, this product has many sizes in the
size field separated by a comma.
In order to import each size as its own attribute we need to use the
field_delimiter property and set it equal to a comma:
$ai->field_delimiter = ',';
So our code will look like this:
Now when our Product Set updates, it will import multiple attributes for the size field instead of just one value.
Using Preferred Attribute Names and Attribute Name Variants
So far we've been importing the attribute name as the merchant has provided it. But what if you want more control.
For example, what if you want to use the word Purple for a product's color instead of the word Violet.
To add preferred attribute names and map them to their variants we can do something like this:
$ai->add_term( "Black", [ "ebony", "onyx", "obsidian" ] ); That code will import the attribute name "Black" when the color is either "ebony" or "onyx" or "obsidian".
Here's an example where we set the preferred color name and its respective variants when importing the
Now when we update our Product Set, it will only import our preferred color names as attributes if the preferred color name or its variants appear in the product's
Adding a Fallback Field When Attribute Variants Aren't Found
Given the above example, if the
color field contains one of our preferred terms or variant terms, then the preferred term will be imported.
But what happens if the
color field does not contain one of the terms we've defined? Without a fallback field, no color will be imported.
In this example, we will provide a a fallback field so that the value from the
color field still gets imported even if it doesn't exist in our list of terms.
For example, the following products has "orange" in the
color field however none of our terms take the word "orange" into account. But, we still want to import it!
Here's how to provide a fallback field:
Now after our Product Set updates the term "orange" will be added to our Color attribute despite the term not being defined in our preferred or variant attribute names.
Import Attribute From Multiple Fields with Preferred Terms
Not every product will have the value you want to import in the field you would expect. But if we have our preferred terms and their variants, we can use multiple fields to extract the values we want.
For example, let's say we have our preferred color names (like in the examples above) but the products we are importing do not have a dedicated
color field. No problem, we can use other fields to deal with products like these:
In these 3 products, we have colors in the
tags field and the
To "look at" all of those field when we import the product and map them to our preferred terms, we can concatenate those fields into one field using a "." and provide a fallback field if needed.
Now after your Product Sets update, your colors should be imported properly.
Providing a Default Value if No Terms Found
Not every product has the attribute value you are targeting. Sometimes you want to provide a default value if not term was found.
For example, the following product is not associated with a Gender. So in that case, we want to use the term "unisex" to indicate that the product is appropriate for both men and women.
Here's how we set a default value if no matches were found.
Now once your Product Sets update, any product which does not contain a gender field will have the default value of "Unisex" applied to its Gender attribute.
Preferred Terms, Term Variants and Excluded Terms
One some occasions you might not want your Preferred Term to be associated with a product if that product contains some other word.
For example, let's say you have an "Age Group" attribute where you want to associate each product with either: Girls, Boys, Mens or Womens. So you write something like this:
$ai->add_term( "Womens", [ "female", "womens", "woman", "women" ] );
But what happens if we have a product like the following:
For this product, our "Girls" product will be imported under the "Womens" Age Group because the
gender field is set to "female".
In cases like these, we need to set some terms to exclude from our preferred "Womens" term. In this case, we will exclude "girl" and "girls" and "kids" from this term like this:
$ai->add_term( "Womens", [ "female", "womens", "woman", "women" ], [ "girl", "girls", "kids" ] );
So our final "Age Group" code will look like this:
Now after your Product Sets update, your Age Group attributes will be imported with the proper values.
Examples, Examples, Examples
You can find a bunch of pre-made attribute importers here. Feel free to use any/all of them! https://gist.github.com/EricBusch/fbe9f7164ec0d4a85d70d0f430581e9a