How to Add an Attribute in Magento 2?


Posted April 2, 2021 by EnvisioneCommerce

While developing a Magento extension, if there is a need to add an attribute programmatically in Magento 2, it is really easy as compared to Magento 1.x.

 
Since Magento 2 features a more developer-friendly’ architecture and offers much more convenient methods to interact with the core & keep away from different kinds of conflicts. Magento 2 has no code pools, so there is no chance of any dispute regarding the right place where to put the code. Following is the right path to put the main files of extension:

app/code/[VendorName]/[ExtensionName]
Crate an extension in this directory based on the above:

app/code/Eecom/CustomAttribute/
A file named as module.xml in the app/code/ Eecom/CustomAttribute directory is required to define our new module by the means of following content:
















As you can observe that XML files in Magento 2 features schema definitions that permit to validate your XML in different positions. The node is used to express your extension’s dependencies, same like node is used in Magento 1.x. Now, it is our next step where we require developing the data script through the parameters of the new attribute:
-- END ---
Share Facebook Twitter
Print Friendly and PDF DisclaimerReport Abuse
Contact Email [email protected]
Issued By Envision eCommerce
Country India
Categories Marketing , Software , Technology
Last Updated April 2, 2021