Adding a product attribute is one of the most popular operations in Commerce. Attributes are a powerful way to solve many practical tasks related to a product. There is a simple process of adding a dropdown-type attribute to a product.
In this video:
Adding a New Attribute to a Product. Adding a product attribute is one of the most popular operations in Magento for both 1 and 2. Attributes are powerful way to solve many practical tasks related to a product. This is quite a broad topic, but in this video we will discuss the simple process of adding a drop-down type attribute to a product. For this exercise, assume that the sample data set is installed. We will add an attribute called clothing_material with the possible values cotton, leather, silk, denim, fur, and wool. We will make this attribute visible on the product view page in bold text. We will assign it to the default attribute set and add a restriction that any bottom clothing, like slacks, cannot be the material, fur. So we’ll need to take the following steps to add the new attribute. One, create a new module, two, add an InstallData script, three, add a source model, four, add a backend model, five, add a frontend model, and six, execute the InstallData script and verify that it works.
Let’s go through each step.
One, create a new module. As Magenta is modular based, we start the process by creating a new module called Learning_ClothingMaterial.
You’ll create two files, etc module.xml and registration.php. We see module.xml here and registration.php here.
Number two, create an InstallData script.
Next, we need to create the InstallData script because adding an attribute technically adds records into several tables, such as eav_attribute and catalog_eav_attribute. This is data manipulation, not a schema change, therefore, we use InstallData instead of InstallSchema. So create the file app code \Learning\ClothingMaterial\Setup\InstallData. Let’s take a minute to look at the code. First of all, we need to use a special setup object, not the one that comes in as a parameter. This is because catalog is an EAV Entity. So to add an attribute, we have to use the $eavSetup rather than the standard one. This holds true for any EAV Entity in Magento 2, such as category, product, customer, and so on. This is why we added EavSetupFactory in a constructor.
For the install method, all we have to do is call the addAttribute method with three parameters, entity type, attribute code, and properties. Those properties define how an attribute behaves. A full list of properties can be seen in the catalog_eav_attribute and eav_attribute tables. Note that there is a mapping between the fields in those tables and the properties in the addAttribute method. To see all the mappings, you should look at Magento/Catalog/Model/ResourceModel/ Setup/PropertyMapper class. For now, we’ll just quickly go through the most important ones. Group means that we add the attribute to the attribute group General, which is present in all attribute sets. Type: Varchar means that the values will be stored in the catalog eav_varchar table. Label: A label of the attribute, that is how it will be rendered in the backend and on the frontend. Source, frontend, and backend: These are special classes associated with the attribute. A source model provides a list of options, frontend defines how it should be rendered on the frontend, and backend allows you to perform certain actions when an attribute is loaded or saved. In our example, it’ll be validation. Global defines the scope of its values, global website or store. visible_on_front is a flag that defines whether an attribute should be shown on the More Information tab on the frontend. is_html_allowed_on_front defines whether an attribute value may contain HTML.
Step three, add a source model.
Next, we need to create the source model under app, code, Learning, ClothingMaterial, Model, Attribute, Source, Material.
As the name implies, the getAllOptions method provides a list of all available options.
Step four, add a backend model.
Now we’ll create a backend model. We’ll put it in app, code, Learning, ClothingMaterial, Model, Attribute, Backend, Material.php.
In our example, we implement only the validate method.
The backend model may have beforeSave, afterSave, and afterLoad methods that allow the execution of some code at the moment an attribute is saved or loaded. The backend model is what makes attribute management a really powerful method of customization.
Note that we hard coded the AttributeSetId here for the sake of time. In other cases, it could be different. Make sure to check the eav_attribute_set table for the right ID.
Step five, add a frontend model.
And, finally, we create a frontend model to make our value bold.
As with the backend model, this is also a very simple class.
Now we can run our code and check the results.
After you run this, the new attribute should have been added to the database.
We see clothing_material down here, the primary key of 155, and its corresponding entry and catalog_eav_attribute with the primary key being 155.
Now let’s go to the backend, open any configurable product, and we should see that clothing_material drop-down.
We’ll set our filters to be a Configurable Product with the attributes set at Bottom.
We’ll select the first item.
First, we’ll set the Clothing Material to Fur and attempt to save our attribute.
We see that our backend model is executed successfully. So now we’ll set it to wool and save it.
Having saved the product, we’ll now move to the frontend.
It should be visible and in bold text. -