Creating EAV Product Attributes in Magento 2 Programmatically
Everything that follows will go in the
// @todo put attributes here bit.
There are a number of entities EAV attributes can be added to; products, categories and customers. We will add a new product attribute so we need this bit of code;
$entityTypeId = $eavSetup->getEntityTypeId(\Magento\Catalog\Model\Product::ENTITY);
We need this because when we make the call to addAttribute() the first parameter is what entity we want to attach the attribute to.
We then need to know specifics about the attribute.
We do that by defining an array of key value pairs defining the attribute.
The key ‘author’ is what we will use as the code of the attribute.
I like to set it up so I can define multiple attributes if I need to and then just loop over them adding them one by one.
As you can see there are quite a few things you can define about an attribute. Many can be ommitted if you do not need to change them from the defaults.
Couple of interesting ones though.
class is useful for adding validation rules to your input.
backend is a string path to a class that controls the saving of the attribute. By default this is not required but for a more
complex attribute, a multiselect for example, the data needs to be saved differently. In the multiselect case the chosen options
need to be imploded together in a string. The class
Magento\Eav\Model\Entity\Attribute\Backend\ArrayBackend helps do that on save.
If you do specify a backend model, it needs to extend
If you wish to control more precisely how the attribute is displayed, you would want to make use of frontend. Like backend
you would define a string class to be used. The class in this case should extend
user_defined and group are important because how these are defined determines how the attribute gets added to the existing attribute sets.
Take a look at this piece of code
You will find the above piece of code in the
addAttribute() function of the
Magento\Eav\Setup\EavSetup class we are using to add our custom attribute.
I find this quite strange behaviour.
If we wanted to only add it to a specific attribute set we need to not specify a group and tell it that our attribute is user_defined.
We would then have to add the attribute to a group (either manually or programmatically) and an attribute set ourselves.
Probably not an issue.
Oh also, if you don’t specify either it will add it to every attribute set under the general group (General as defined in the EavSetup class).
By the way, if you do specify a group and that group currently doesn’t exist, Magento will create it for you automatically.
See a working code example over at github
Like this post? Share it :)